C # XML Parsing Method Instance resolution (with namespace)

Source: Internet
Author: User

 

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.

  1. Protected VoidLoadxmlfile (fileinfo xmlfile)
  2. {
  3. If(Xmlfile =Null|! Xmlfile. exists)
  4. {
  5. Throw NewFilenotfoundexception (
  6. String. Format ("The file to be parsed does not exist {0 }. ",
  7. Xmlfile. fullname ));
  8. }
  9. // Load the file
  10. This. Doc =NewXmldocument ();
  11. Doc. Load (xmlfile. fullname );
  12. // Prepare to read the file
  13. Root = Doc. documentelement;
  14. StringNamespace = root. namespaceuri;
  15. Nsmgr =NewXmlnamespacemanager (Doc. nametable );
  16. Nsmgr. addnamespace ("Ns", Namespace );
  17. }

◆ Note the use of xpath for C # XML parsing.

A. These two lines are used to obtain the namespace of the XML document.

    1. Root = Doc. documentelement;
    2. StringNamespace = root. namespaceuri;

B. Create the namespace manager for the XML document.

    1. Nsmgr =NewXmlnamespacemanager (Doc. nametable );
    2. 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.

  1. Protected StringGetnodevalue (
  2. StringPrefixpath,StringXrelativepath)
  3. {
  4. If(Doc =Null)
  5. {
  6. Loadxmlfile (xmlfileinfo );
  7. }
  8. StringXPath =String. Empty;
  9. If(!String. Isnullorempty (xrelativepath ))
  10. {
  11. If(!String. Isnullorempty (prefixpath ))
  12. {
  13. XPath = prefixpath + xrelativepath;
  14. }
  15. Else
  16. {
  17. XPath = xrelativepath;
  18. }
  19. }
  20. XPath = XPath. Replace ("/","/Ns :");
  21. Xmlnode node = root. selectsinglenode (XPath, nsmgr );
  22. If(Node =Null)
  23. {
  24. Return Null;
  25. }
  26. ReturnNode. innerxml;
  27. }

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:

    1. 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.

  1. <? XMLVersion="1.0" Encoding=UTF-8"? >
  2. <RootXmlns="Urn: classnamespace">
  3. <Class>
  4. <Classid> 1234 </classid>
  5. </Class>
  6. <Students>
  7. <Student>
  8. <ID> 01 </ID> <Name> name01 </Name>
  9. </Student>
  10. <Student>
  11. <ID> 02 </ID> <Name> name02 </Name>
  12. </Student>
  13. </Students>
  14. </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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.