Kirk Evans Blog

.NET From a Markup Perspective

What's Wrong With XmlDocument?

What's Wrong With XmlDocument?

  • Comments 1

So, what's wrong with System.Xml.XmlDocument in .NET?  Plenty, according to Joshua Prismon:

To use XMLDocument and Datasets, you have to have the full tree in memory.

If you are concerned about loading only branches of data at a time, the System.Xml namespace provides all the classes that you could possibly need.  And for the ones you dream up, the System.Xml namespace provides a wealth of flexibility.

XmlTextReader reader = new XmlTextReader(Server.MapPath("data/mediumdoc.xml"));
reader.MoveToContent();
while(reader.Read())
{
  string id = reader.GetAttribute("id");
  if(null != id && id.CompareTo("3") == 0)
  {
    XmlDocument doc = new XmlDocument();
    doc.AppendChild(doc.ReadNode(reader));

    //Do something interesting with DOM model here
    Response.ContentType = "text/xml";
    Response.Write(doc.OuterXml);
  }
}
reader.Close();

 

  • May I also recommend setting the WhitespaceHandling property for even better performance from XmlTextReader as in:
    reader.WhitespaceHandling = WhitespaceHandling.None

Page 1 of 1 (1 items)
Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post
Translate This Page
Search
Archive
Archives