在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
<?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>[email protected]</aws:Email> </aws:ContactInfo> <aws:Alexa> </aws:UrlInfoResult> </aws:Response> </aws:UrlInfoResponse> 假如你要取得<aws:Email>节点,诶,你发现<aws:UrlInfoResponse>和<aws:Response>节点的命名空间马甲是一样的。 这个时候怎么办呢?难道是添加两次aws命名空间?其实不然。做法如下: 1.我们只要添加<aws:Response>节点的命名空间。 xnm.AddNamespace("aws", "http://awis.amazonaws.com/doc/2005-07-11"); 2.取得根节点,这样我们就可以忽略根结点的命名空间,然后应用XPath取得你想要的节点。 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);
这样我们就可以得到<aws:Email>节点了,然后打印出来 foreach (XmlNode xn1 in xnl) { Response.Write(xn1.InnerXml.ToString() + "</br>"); } 完整代码: 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>"); } -------------------------------------------------------- 譬如要讀到DataSet中,如:
|
请发表评论