ASP.NET System.Xml.XmlTextWriter problem.

Izi

Izi

Soldato
Joined
9 Dec 2007
Posts
2,718
I have XML writer function:

Code:
        public XmlTextWriter BuildFeed(RssItemCollection collection)
        {
            XmlTextWriter xml = new XmlTextWriter(HttpContext.Current.Response.OutputStream, Encoding.UTF8);
            xml.WriteStartDocument();
            xml.WriteStartElement("rss");
            xml.WriteAttributeString("version", "2.0");
            xml.WriteStartElement("channel");
            xml.WriteElementString("title", "title");
            xml.WriteElementString("link", "link");
            xml.WriteElementString("description", "description.");         

            foreach (RssItem item in collection)
            {
                xml.WriteStartElement("item");
                xml.WriteElementString("title", item.RssTitle);
                xml.WriteElementString("description", item.RssTitle);
                xml.WriteElementString("link", item.RssTitle);
                xml.WriteElementString("pubDate", item.RssPubDate.ToString("R"));
                xml.WriteEndElement();
            }

            xml.WriteEndElement();
            xml.WriteEndElement();
            xml.WriteEndDocument();
            xml.Flush();
            xml.Close();

            return xml;
        }

I am then using ashx to call method:
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();

RssItem item = new RssItem();
item.RssDescription = "description";
item.RssGuid = "newguid";
item.RssPubDate = DateTime.Now;
item.RssTitle = "title";

RssItemCollection rsscollection = new RssItemCollection();
rsscollection.Add(item);

RssManager manager = new RssManager();


context.Response.ContentType = "text/xml";
context.Response.Write((manager.BuildFeed(rsscollection)).);
context.Response.End();
}

public bool IsReusable
{
get
{
return false;
}
}

Works fine, but appends "System.Xml.XmlTextWriter" to the end of my xml.

What am I doing wrong?
 
You are writing the writer to the context, that's why. You're not writing the stream it has written to, to the context. :)

Given you are using the current context as the stream for the writer anyway, you don't need to have the line
Code:
context.Response.Write((manager.BuildFeed(rsscollection)).);
(which has an erroneous '.' anyway...) just:
Code:
manager.BuildFeed(rsscollection);

thanks Jester...

context.Response.Write(xml.BaseStream);

worked.

Should this method be static or instantiated?
 
That'll write the XML twice, you are already writing the XML to the context once within your BuildFeed method. A Writer writes it doesn't just store. :)

I wouldn't use static for that.



Yeah, posted that before your edit and other post.

What you wrote makes sense, muchos thankos.

Just out of interest, at what point is it better to use instantiated methods? Is it when the class has lots of methods which need to talk to each other?
 
What is the best way to have strongly typed access to a class?

Instead of saying Class class = new Class() Can I just say Class.Instantiate.Method?
 
Back
Top Bottom