C # manipulating XML with namespaces

Source: Internet
Author: User

The previous operation of XML is generally done in the following way:

The advantage is that XDocument can use LINQ

        Xmlpath = "path";             = Xdocument.load (xmlpath);            IEnumerable from in Myxdoc.descendants ("signal")                                                the where target. Attribute ("signalname"). Value = =                                                nameSelect  target            ; = Targetnodes.first ();

This only way encountered with the name space on the pit, it seems that the reference to the two blog, found that the following methods can be manipulated

StreamReader reader=new StreamReader ("path");
XmlReader reader =xmlreader.create (Respstream); XElement Root=xelement.load (reader); XmlNameTable nameTable=Reader. NameTable; XmlNamespaceManager NamespaceManager=NewXmlNamespaceManager (nameTable); Namespacemanager.addnamespace ("WFS","Http://www.opengis.net/wfs"); Namespacemanager.addnamespace ("ows","http://www.opengis.net/ows"); Namespacemanager.addnamespace ("OCG","HTTP://WWW.OPENGIS.NET/OCG"); Namespacemanager.addnamespace ("XSI","http://www.w3.org/2001/XMLSchema-instance"); Namespacemanager.addnamespace ("GML","HTTP://WWW.OPENGIS.NET/GML"); Namespacemanager.addnamespace ("OGC","HTTP://WWW.OPENGIS.NET/OGC"); Namespacemanager.addnamespace ("XLink","Http://www.w3.org/1999/xlink"); Namespacemanager.addnamespace ("MyWorld","http://www.myWorld.com"); XElement Child1= root. Xpathselectelement ("./wfs:featuretypelist", NamespaceManager); XElement child2= Child1. Xpathselectelement ("./wfs:featuretype", namespacemanager);//Here is the point, child2 since it is a child node of Child1, Child1 's Xpathselectelement method must be called

The above method also saw, if Child2 is child1 child node, you must call Child1 Xpathselectelement method, so if you want to take a deep depth of XElement, is not to manually find its parent node, Then use the above method to write a bunch of ...

Child. Xpathselectelement ()

Thinking of the method of finding the specified node in xmal in WPF, I used recursion to write the following methods to get the child nodes.
Call GetNode, the passed argument is the previous piece of code, and the last Xelementlist is the collection of XElement that needs to be fetched.

If you use the code in the comment, the first retrieved XElement in the XML document is returned.
        Listnew list<xelement>();

<summary>
Get child nodes (imperfect, the first level of the root dot can not be obtained, if necessary, you need to call XElement elelment = root. Xpathselectelement (Nodepath, XmlNamespaceManager);)
</summary>
<param name= "root" > Root node </param>
<param name= "XmlNamespaceManager" > Namespace manager (I call it myself) </param>
<param name= "Nodespace" > The namespace of the node to be acquired </param>
<param name= "NodeName" > The name of the node you want to get, such as <ows:dcp>,ows is the namespace, the DCP is its name </param>
<returns></returns>

PrivateXElement getnode (XElement root,xmlnamespacemanager XmlNamespaceManager,stringNodespace,stringnodeName) {List<XElement> childlist =Root. Elements ().            ToList ();  for(inti =0; i < Childlist.count; i++) {XElement child=Childlist[i]; varnode =getelement (Child, XmlNamespaceManager, Nodespace, nodeName); if(Node! =NULL) {Xelementlist.add (node); //return node;                }                Else{node=getnode (Child, XmlNamespaceManager, Nodespace, nodeName); if(Node! =NULL) xelementlist.add (node); //return node;                }            }            return xelementlist;
return node;
}

Private XElement getelement (XElement root, XmlNamespaceManager XmlNamespaceManager, String nodespace, String  NodeName)        {            var enumerator = xmlnamespacemanager.getenumerator ();            While (enumerator. MoveNext ())            {                var nameSpace = Enumerator. Current.tostring ();                If (Namespace.equals (nodespace))                {                    string nodepath = "./" + NameSpace + ":" + nodeName; XElement elelment = root. Xpathselectelement (Nodepath, XmlNamespaceManager); if (elelment! = null) return elelment;}} return null;}        


Reference:

Http://www.cnblogs.com/HQFZ/p/4788428.html

Http://www.cnblogs.com/landeanfen/p/4636102.html

C # manipulating XML with namespaces

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.