1. General treatment
XmlDocument xmldoc =NewXmlDocument (); Xmldoc.loadxml ("<?xml version=\ "1.0\" encoding=\ "utf-8\"? >"); XmlNode RootNode= Xmldoc.selectsinglenode ("HH"); foreach(XmlNode Xxnodeinchrootnode.childnodes) {stringDSF =Xxnode.innertext; stringSDF =Xxnode.name; }
2. Using a DataTable
DataSet Dsdata =NewDataSet (); Dsdata.readxml (NewXmlTextReader (NewStringReader ("<?xml version=\ "1.0\" encoding=\ "utf-8\"? >"))); DataTable DT= dsdata.tables["HH"]; foreach(DataRow Drinchdt. Rows) {foreach(DataColumn DCinchDr. Table.columns) {stringn =DC. ColumnName; stringValue =Dr[n]. ToString (); } }
3. XML text parsing with namespaces (NameSpace)
<?XML version= "1.0" encoding= "Utf-8"?><Bookstorexmlns= "Http://www.lucernepublishing.com"Xml:base= "Http://www.semanticweb.org/ontologies/2012/2/OntologyMinePower.owl"> < Book> <title>C language</title> <authorname= "XIAO"> < Age>25</ Age> </author> < Price>50</ Price> </ Book> < Book> <title>Database</title> < Price>100</ Price> </ Book> < Book> <title>Data</title> < Age>100</ Age> <authorname= "Wang"> < Age>25</ Age> <Sex>Female</Sex> </author> < Price>66.5</ Price> </ Book></Bookstore>
private void Button1_Click (object sender, EventArgs e) { XmlDocument doc = new XmlDocument (); Doc. Load (@ ": \.. \cd.xml "); XmlElement root = null; root = Doc. documentelement; XmlNamespaceManager nsmgr = new XmlNamespaceManager (Doc. NameTable); Nsmgr. AddNamespace ("AB", "http://www.lucernepublishing.com"); XmlNodeList listnodes = null; Listnodes = doc. SelectNodes ("/ab:bookstore/ab:book[ab:author[ab:sex]]/ab:price", nsmgr); foreach (XmlNode node in listnodes) { richTextBox1.Text + = node. InnerText + "\ n"; } }
The XPath language means to query all bookstore with author nodes, and the author node has the price child node of the book node with the sex node. The result is 66.5.
For parsing of XML literals with namespaces, simply call the different overloaded functions of xmlnode.selectnodes, give the XmlNamespaceManager parameter, and adjust the XPath statement accordingly.
Detailed reference
1. Here I refer to MSDN for an XML source file, in order to show different effects, the XML structure is logically unreasonable
<?XML version= "1.0" encoding= "Utf-8"?><Bookstore> < Book> <title>C language</title> <authorname= "XIAO"> < Age>25</ Age> </author> < Price>50</ Price> </ Book> < Book> <title>Database</title> < Price>100</ Price> </ Book> < Book> <title>Data</title> < Age>100</ Age> <authorname= "Wang"> < Age>25</ Age> <Sex>Female</Sex> </author> < Price>66.5</ Price> </ Book></Bookstore>
2. Simple example
Private voidButton1_Click (Objectsender, EventArgs e) {XmlDocument doc=NewXmlDocument (); Doc. Load (@".. \.. \cd.xml"); XmlElement Root=NULL; Root=Doc. DocumentElement; XmlNodeList Listnodes=NULL; Listnodes= root. SelectNodes ("/bookstore/book/price"); foreach(XmlNode nodeinchlistnodes) {richTextBox1.Text+ = node. InnerText +"\ n"; } }
Here listnodes=root. SelectNodes ("/bookstore/book/price") means to select all the price nodes that conform to the Bookstore-->book-->price-level relationship
Effects such as:
3.XPath explanation
1)"/bookstore/book/price"
The beginning of the XPath is a slash (/) to indicate that this is an absolute path, which can select all the paths that conform to this pattern of elements. This represents the price element under all the book elements under the bookstore root node.
2)"//title"
Starting with two slashes (//) means that all elements in the file that conform to the pattern will be selected, even at different levels in the tree. This means that all title elements are selected.
3)"/bookstore/book/author/*"
Use an asterisk (*) to select an unknown element. This represents the selection of all possible nodes under the Anthor node under the book node under the bookstore root node.
Results obtained: female
4)"/bookstore/book/*/age"
The syntax above will select the age node at the other level of the book node under all bookstore root nodes.
The results are: 25 25
5)"/*/*/age"
Select the age element with two levels to get the result: 100
Note that in order to access elements that are not hierarchical, theXPath syntax must begin with two slashes (//), with an asterisk (*) to access the unknown element, the asterisk can only represent an element of unknown name, and cannot represent an element of an unknown level.
6)"bookstore/book[1]/title"
Use the brackets to select a branch. This represents selecting the title node of the first book node under the bookstore root node.
The results are: C language
7)"bookstore/book[last ()]/title"
Again, this means selecting the last node, resulting in: data structure
8)"Bookstore/book/author[sex]"
This means selecting all author nodes with a sex element and satisfying a certain hierarchy, resulting in: 25female
9)"/bookstore/book[price=66.5]/title"
Select the value of the price element is 66.5 of the book node's title child node, the result is: data structure
)"/bookstore/book/title | /bookstore/book/price "
Using the or operand (|) You can select more than one path. This means selecting the title element and the price element that conform to a certain hierarchy
The result is: C language 50 database 100 data structure 66.5
One)"//@name"
In XPath, in addition to selecting an element, you can also select the attribute, which begins with @. This means that all the name properties are selected.
The result is: XIAO Wang
"//author[@name]/age"
This indicates that all the age nodes of the author node with the name attribute are selected. The result is: 25 25
"//*[@*]"
Select all nodes that have attributes. The result is: 25female
"//author[@name = ' Wang ']/sex"
Select the sex child node of the author node with all properties name value Wang. The result is: female
C # (. Net) parsing xml