How to build XmlNodes from XmlReader How to build XmlNodes from XmlReader xml xml

How to build XmlNodes from XmlReader


Why not just do the following?

XmlDocument doc = new XmlDocument();XmlNode node = doc.ReadNode(reader);


The XmlNode type does not have a public constructor, so you cannot create them on your own. You will need to have an XmlDocument that you can use to create them:

XmlDocument doc = new XmlDocument();while (xmlReader.Read()){    //keep reading until we see my element    if (xmlReader.Name.Equals("myElementName") && (xmlReader.NodeType == XmlNodeType.Element))    {        // How do I get the Xml element from the reader here?        XmlNode myNode = doc.CreateNode(XmlNodeType.Element, xmlReader.Name, "");        nodeList.Add(myNode);    }        }


XmlReader and XmlDocument have a very distinct way of processing. XmlReader keeps nothing in memory and uses a forward-only approach as opposed to building a full DOM tree in memory for XmlDocument. It is helpful when performance is an issue, but it also requires you to write your application differently: instead of using XmlNode, you don't keep anything and only process "on the go": i.e., when an element passes by that you need, you do something. This is close to the SAX approach, but without the callback model.

The answer to "how to get the XmlElement" is: you'll have to build them from scratch based on the info from the reader. This, unfortunately, defies the performance gain. It is often better to prevent using DOM approaches altogether once you switch to XmlReader, unless for a few distinct cases.

Also, the "very handy" way to extract nodes using XPath (SelectNodes is what you show above) cannot be used here: XPath requires a DOM tree. Consider this approach a filtering approach: you can add filters to the XmlReader and tell it to skip certain nodes or read until a certain node. This is extremely fast, but a different way of thinking.