When I researched the topic of my recent post about the XmlSerializer I became aware that there are more ways to process XML data in .Net than I knew about. Here is an overview of how you can access XML data in .Net.
XML data
The .Net framework provides the following classes to store XML data in memory:
- XSD proxy classes
XML serialization
The .Net framework implements a set of dependent classes to serialize and deserialize XML data. Each class in this list can be instantiated using a parameter of the classes below it:
- TextReader (implemented by StreamReader), TextWriter (implemented by StreamWriter)
- Stream (implemented by FileStream)
- filename (string)
XML query and manipulation
Besides the classes named above, other classes exist to query and manipulate XML data:
Putting it all together
Data model | Access (MSDN summary) | Serialization | XML Query |
XmlDocument | “Represents an XML document.” In-Memory |
OuterXml LoadXml() Load(XmlReader), Save(XmlWriter) |
XPath SelectSingleNode(), SelectNodes(); Linq To Objects; XPathNavigator CreateNavigator() |
XDocument | “Represents an XML document.” In-Memory |
Load(XmlReader), Save(XmlWriter), Parse() |
XmlReader CreateReader(); Linq To Xml; XPathNavigator CreateNavigator() |
XSD proxy classes | In-Memory | XmlSerializer, Introduction on MSDN |
Linq To Objects |
XmlReader | “a reader that provides fast, non-cached, forward-only access to XML data.” | Create(…) | GetAttribute(), MoveTo*(), Read*() |
XmlNodeReader | “a reader that provides fast, non-cached forward only access to XML data in an XmlNode.” | (XmlNode) | derived from XmlReader |
XPathNavigator | “a cursor model for navigating and editing XML data.” “read-only if created by XPathDocument; editable if created by XmlDocument“ |
(CreateNavigator()) | GetAttribute(), Value*, MoveTo(); XPath Select(), Select*(); XmlReader ReadSubtree() |
XPathDocument | “a fast, read-only, in-memory representation of an XML document” | Constructor() | XPathNavigator CreateNavigator() |
Conversion and Comparison
MSDN
Converting from XmlDocument to XDocument
Comparing XmlReader to SAX Reader
StackOverflow
Use XDocument as the source for XmlSerializer.Deserialize?
Converting XDocument to XmlDocument and vice versa
XMLSerializer vs XMLReader vs XMLDocument vs XDocument Performance Comparison
Other
XmlDocument versus XDocument versus XmlReader/XmlWriter
Pingback: Parsing and Analyzing OpenStreetMap Export Files « devioblog