C # retrieve XML with namespace

Source: Internet
Author: User
<? XML version = "1.0"?> <AWS: urlinforesponse xmlns: AWS = "http://alexa.amazonaws.com/doc/2005-10-05/"> <AWS: Response xmlns: AWS = "http://awis.amazonaws.com/doc/2005-07-11"> <AWS: urlinforesult> <AWS: Alexa> <AWS: contactinfo> <AWS: email> baidu_dns_master@baidu.com </AWS: email> </AWS: contactinfo> <AWS: Alexa> </AWS: urlinforesult> </AWS: response> </AWS: urlinforesponse> if you want to obtain the <AWS: email> node, you find that <AWS: urlinforesponse> and <AWS: response> The namespace vest of the node is 1. .
What should we do at this time? Is it adding AWS namespaces twice? Actually not. The procedure is as follows:
1. We only need to add the namespace of the <AWS: Response> node.
xnm.AddNamespace("aws", "http://awis.amazonaws.com/doc/2005-07-11");
2. Get the root node. In this way, we can ignore the root node namespace and apply XPath to get the desired node.
XmlElement xe = xd.DocumentElement;XmlNodeList xnl = xe.SelectNodes("//aws:Response/aws:UrlInfoResult/aws:Alexa/aws:ContactInfo/aws:Email", xnm);
You can also directly retrieve the data as follows:
XmlNodeList xnl = xd.SelectNodes("//aws:Response/aws:UrlInfoResult/aws:Alexa/aws:ContactInfo/aws:Email", xnm);
In this way, we can get the <AWS: email> node and print it out.
foreach (XmlNode xn1 in xnl){Response.Write(xn1.InnerXml.ToString() + "</br>");}
Complete code:
XmlDocument xd = new XmlDocument();xd.Load(Server.MapPath(".")+"/a.xml");XmlNamespaceManager xnm = new XmlNamespaceManager(xd.NameTable);xnm.AddNamespace("aws", "http://awis.amazonaws.com/doc/2005-07-11");XmlElement xe = xd.DocumentElement;XmlNodeList xnl = xe.SelectNodes("//aws:Response/aws:UrlInfoResult/aws:Alexa/aws:ContactInfo/aws:Email", xnm);//XmlNodeList xnl = xd.SelectNodes(//"//aws:Response/aws:UrlInfoResult/aws:Alexa/aws:ContactInfo/aws:Email", xnm);foreach (XmlNode xn1 in xnl){Response.Write(xn1.InnerXml.ToString() + "</br>");}
--------------------------------------------------------
For example:

               XmlDocument doc = new XmlDocument();
                doc.Load(filename);
                XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
                xnm.AddNamespace("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
                xnm.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");

                XmlNode node = doc.SelectSingleNode("//xml/rs:data", xnm);
                XmlNodeReader reader = new XmlNodeReader(node);
                ds.ReadXml(reader);

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.