I. Reading XML using the XML DOM
This section highlights:
- Several ways to load XML
- Read XML with no namespace
- Filter by using XPath to select the desired node
- Read XML with namespace
The XML DOM approach is the most primitive way to manipulate XML, starting with the. Net Framework 1.0 to support DOM mode.
1. How do I read XML in the form of an XML DOM?
To read XML, the XML is loaded first, in two ways, one from a stream or like reader, and the other is to load a string.
A. Loading from stream or reader, load method has five overloads
XmlDocument doc =NewXmlDocument ();d OC. Load ("D:\\test.xml");using(FileStream stream = File.openread ("D:\\test.xml") ) {doc. Load (stream);}using(TextReader reader = File.OpenText ("D:\\test.xml") ) {doc. Load (reader);}using(XmlReader reader = Xmlreader.create ("D:\\test.xml") ) {doc. Load (reader);}
B. Loading from a string
New XmlDocument ();d OC. LOADXML (@ "<?xml version=" "1.0" "encoding=" "UTf-16" "?><data> <ITEM>ABC </item> <item>123</item> <item>vwxyzh</item></data>" );
2. Read XML with no namespace
The XML is ready, and the XML begins to read.
A. Now you want to read the text of all the item below the data section:
foreach in Doc. childnodes[1]. ChildNodes) { Console.WriteLine (item. InnerText);}
Results:
Abc
123
Vwxyzh
B. Filtering by using XPath
There are a number of problems with the above, such as having a non-item node in the data node, so that the access is not the same as the non-item item also written out.
For example:
<?XML version= "1.0" encoding= "UTf-8"?><Data> <Item>Abc</Item> <Item>123</Item> < Other>[Email protected]#</ Other> <Item>Vwxyzh</Item></Data>
Results:
Abc
123
[Email protected]#
Vwxyzh
Obviously "[Email protected]#" is also selected, this is not what we want, so instead of using XPath to access:
foreach in Doc. SelectNodes ("/data/item")) { Console.WriteLine (item. InnerText);}
Result: The other item is excluded
Abc
123
Vwxyzh
3. Read XML with namespace
Like C #, XML has namespace, and namespace has a huge role in XML.
Let's start by loading an XML fragment:
Doc. LOADXML (@"<?xml version="1.0"encoding="utf-8"?><v:data xmlns:v="Urn:vwxyzh"> <v:item>abc</v:item> <v:item>123</v:item> <v:other>[email protected]#</v:other> <v:item>vwxyzh</v:item> <v:item& Gt the/Ten/ -</v:item></v:data>");
Here we have prepared a namespace-urn:vwxyzh, and abbreviated to V.
Now execute the following procedure according to the original XPath, and you will find that no element is selected. Why is that?
Because the data section in the original "/data/item" is not namespace data, this XPath does not locate any nodes at all.
We have to modify some of the code to achieve our goal, how to do?
A. Let's take a look at the overloads of the SelectNodes method:
Public XmlNodeList selectnodes (string XPath); Public XmlNodeList selectnodes (string XPath, XmlNamespaceManager nsmgr);
The first overload is the one used before;
The second overload, you need to provide a XmlNamespaceManager instance, a look at the name will know that this instance is used to manage the XML namespace.
B. Another look at the XmlNamespaceManager class, you can see that creating this instance requires an instance of type XmlNameTable to do arguments, so who can provide an instance of XmlNameTable?
C. XmlDocument itself provides this xmlnametable:
D. Modify the code:
New XmlNamespaceManager (Doc. NameTable); Xnmgr. AddNamespace ("v""urn:vwxyzh"); foreach in Doc. SelectNodes ("/v:data/v:item", Xnmgr)) { Console.WriteLine (item. InnerText);}
Create an instance of XmlNamespaceManager first, and then use the AddNamespace method to set "V" to "Urn:vwxyzh". Then modify the XPath and change the DAT to V:data,item to V:item, and that's it.
Run to see the result:
Abc
123
Vwxyzh
2009/10/18
C # Operations XML (i)