Http://www.csharpwin.com/csharpspace/3968r4687.shtml
C # steps for XML parsing using XPath:
1. You need to load the document before reading the desired node value.
◆ XML document
Protected xmldocument Doc = NULL;
◆ Root element (node) of the XML document)
Protected xmlelement root = NULL;
◆ Namespace manager for XML documents
Protected xmlnamespacemanager nsmgr = NULL;
2. The next step is to load the document.
- Protected VoidLoadxmlfile (fileinfo xmlfile)
-
- {
-
- If(Xmlfile =Null|! Xmlfile. exists)
-
- {
-
- Throw NewFilenotfoundexception (
-
- String. Format ("The file to be parsed does not exist {0 }. ",
-
- Xmlfile. fullname ));
- }
-
- // Load the file
-
- This. Doc =NewXmldocument ();
-
- Doc. Load (xmlfile. fullname );
-
- // Prepare to read the file
-
- Root = Doc. documentelement;
-
- StringNamespace = root. namespaceuri;
-
- Nsmgr =NewXmlnamespacemanager (Doc. nametable );
- Nsmgr. addnamespace ("Ns", Namespace );
-
- }
◆ Note the use of xpath for C # XML parsing.
A. These two lines are used to obtain the namespace of the XML document.
- Root = Doc. documentelement;
- StringNamespace = root. namespaceuri;
B. Create the namespace manager for the XML document.
- Nsmgr =NewXmlnamespacemanager (Doc. nametable );
- Nsmgr. addnamespace ("Ns", Namespace );
If your XML document has a famous spaceCodeIs indispensable.
3. The next step is to read the value of the document node.
Here, the two input parameters prefixpath are the parent node path of the node, and xrelativepath is the name of the node to be read.
In addition, the xmlfileinfo variable is the XML file to be loaded.
-
- Protected StringGetnodevalue (
-
- StringPrefixpath,StringXrelativepath)
-
- {
-
- If(Doc =Null)
-
- {
- Loadxmlfile (xmlfileinfo );
-
- }
-
- StringXPath =String. Empty;
-
- If(!String. Isnullorempty (xrelativepath ))
-
- {
-
- If(!String. Isnullorempty (prefixpath ))
-
- {
-
- XPath = prefixpath + xrelativepath;
- }
-
- Else
-
- {
-
- XPath = xrelativepath;
-
- }
-
- }
-
- XPath = XPath. Replace ("/","/Ns :");
-
- Xmlnode node = root. selectsinglenode (XPath, nsmgr );
-
- If(Node =Null)
- {
-
- Return Null;
-
- }
-
- ReturnNode. innerxml;
-
- }
Some may ask, why do we need to set two parameters prefixpath and xrelativepath? In fact, this does not have much to do with. I just want to make it easy for myself. You can also determine this XPath outside the method, set only one input parameter in the method, and the effect is the same.
◆ Pay attention to this line:
- XPath = XPath. Replace ("/","/Ns :");
If your XML document contains namespace, this line is indispensable. Otherwise, nodes cannot be found and cannot be parsed.
Some Questions about XPath:
For such an XML document, to find the name of the student under the first node (ID = 01), its XPath should be "/ns: Root/ns: students/ns: student [1]/ns: Name ". For duplicate node names in XML, the order is 1, 2, 3... that is to say, if you want to find the node under the nth student node, you should use the student [N] identification method.
-
- <? XMLVersion="1.0" Encoding=UTF-8"? >
-
- <RootXmlns="Urn: classnamespace">
-
- <Class>
-
- <Classid> 1234 </classid>
-
- </Class>
- <Students>
-
- <Student>
-
- <ID> 01 </ID> <Name> name01 </Name>
-
- </Student>
-
- <Student>
-
- <ID> 02 </ID> <Name> name02 </Name>
-
- </Student>
-
- </Students>
-
- </Root>
Of course, you can also obtain the value of the node attribute and find the node that meets the specific value. These are similar to the process of obtaining the node value above.
The implementation of C # XML parsing through XPath is introduced here. I hope to help you understand and learn about C # XML parsing.