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?
 
Have you debugged your running code?

Try and display your XML stream from the end of BuildFeed before you leave the method. That will help you to work out if the appending is happening from the XML construction or from the rendering in the browser.

I built a very similar RssFeed builder, but never seen that been appended.
 
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);
 
I'd also change the BuildFeed method to accept a stream for a parameter, instead of using the globally accessible context (poor OO design to do that) so:
Code:
public void BuildFeed(IEnumerable<RssItem> collection, Stream stream)
{
  var xml = new XmlWriter(stream);
  //etc..
  foreach (var item in collection)
  {
     //etc..
  }
  //etc, but no need to return a value
}
and use it like so:
Code:
manager.BuildFeed(rsscollection, HttpContext.Curent.Response.OutputStream);

that way you can test it...
 
Last edited:
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.
 
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?
 
If your object has stateful information, then it should be instantiated. If your methods access stateful information (i.e. instance variables) then it should not be static.

However, static methods can, and usually are, a code smell and I will stay away from them wherever possible. Statics need strong justification, imo.
 
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