<? 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);