使用XmlDocument.SelectNodes遍历xml元素遇到的一个XPathException
使用XmlDocument类时候报错:
未处理的XPathException:
需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。
需要使用XmlNamespaceManager类:Resolves, adds, and removes namespaces to a collection and provides scope management for these namespaces.
这是因为使用的soap文档含有自己的命名空间。相同问题:http://bbs.csdn.net/topics/350101606
最终修改后的代码:
string soap = "";
soap += "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">";
soap += "<SOAP-ENV:Body>";
soap += "<AP-MSG:SubscriptionRequest xmlns:AP-MSG=\"http://www.vestas.dk/2001/04/ap\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\">";
soap += "<RequestBase ClientRequestHandle=\"41\" ReturnItemTime=\"false\" ReturnItemName=\"true\" CallbackURL=\"10.116.21.222:1055\" CallbackRate=\"PT10S\"/>";
soap += "<ItemRequestSpec MaxAge=\"PT0.1S\"/>";
soap += "<ItemList>";
foreach (IoTag tag in Tags)
{
soap += string.Format("<Item ItemName=\"{0}\"/>", tag.Name);
}
soap += "</ItemList>";
soap += "</AP-MSG:SubscriptionRequest>";
soap += "</SOAP-ENV:Body>";
soap += "</SOAP-ENV:Envelope>";
string subscriptionRequest = "";
subscriptionRequest += "POST * HTTP/1.1\r\n";
subscriptionRequest += "Content-Type:text/xml;charset=ISO-8859-1\r\n";
subscriptionRequest += "Connection:Keep-Alive\r\n";
subscriptionRequest += "SOAPAction:http://www.vestas.dk/2001/04/ap#SubscriptionRequest\r\n";
subscriptionRequest += string.Format("Content-Length:{0}\r\n", soap.Length);
subscriptionRequest += "\r\n";
subscriptionRequest += soap; XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(soap);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xDoc.NameTable);
nsMgr.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
nsMgr.AddNamespace("AP-MSG", "http://www.vestas.dk/2001/04/ap");
nsMgr.AddNamespace("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
XmlElement xe = xDoc.DocumentElement;
XmlNodeList xnl = xDoc.SelectNodes("/SOAP-ENV:Envelope/SOAP-ENV:Body/AP-MSG:SubscriptionRequest/ItemList/Item", nsMgr); foreach (XmlNode item in xnl)
{
string s = item.Attributes["ItemName"].Value;
}
使用XmlDocument.SelectNodes遍历xml元素遇到的一个XPathException的更多相关文章
- STL中用erase()方法遍历删除元素 .xml
pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...
- C# 使用XmlDocument类对XML文档进行操作
原创地址:http://www.cnblogs.com/jfzhu/archive/2012/11/19/2778098.html 转载请注明出处 W3C制定了XML DOM标准.很多编程语言中多提供 ...
- [VBS]遍历XML文档
一.脚本源码 使用VBS脚本遍历XML文件 Option Explicit '定义常量 '元素 '文本 'CDATA '实体引用 '实体 '处理指令 '注释 '文档类型 '遍历文件 NameList. ...
- 递归遍历XML节点属性和属性值
public static XmlDocument FileMergedIntoXML(string strXmlPathPublic) { string strXmlPathPublic = str ...
- XML DOM 遍历Xml文档
1.xml文档内容: <?xml version="1.0" encoding="utf-8" ?> <bookstore> <b ...
- 递归遍历XML所有节点
package xml; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.DocumentEx ...
- JavaScript遍历XML总结
1:读取服务器端xml(注意不同浏览器版本的区别),使用XML可以增强系统的扩展性,只用修改XML就可以实现增加减少功能的目的. function loadXMLDoc1(dname){ if ...
- 详解SimpleXML添加_修改_删除_遍历XML节点属性
SimpleXML概述 要处理XML 文件,有两种传统的处理思路:SAX 和DOM.SAX 基于事件触发机制,对XML 文件进行一次扫描,完成要进行的处理:DOM 则将整个XML 文件构造为一棵DOM ...
- 问题:jQuery中遍历XML文件时候,获取子节点children不支持的情况(已解决)
问题描述: 今天在写一个基于 jquery 的读取xml文件的程序时候,需要遍历xml的节点. 代码片段如下: function parse_xml_node(parent,result){ // r ...
随机推荐
- HDU1016(bfs)
import java.util.Scanner;public class Main1016 { public static void main(String[] args) { Scanner ci ...
- [Form Builder]:CREATE_GROUP Built-in
Description Creates a non-query record group with the given name. The new record group has no colum ...
- 常见资源记录定义(Resource Record)
所有的RRs(Resource Records)都具有相同的顶级字段格式定义:owner TTL CLASS TYPE RDATA owner 指示拥有资源记录的DNS域名 TTL 对大多数资源记录 ...
- maven主仓库中找不到restlet的解决办法
解决办法: 修改Pom.xml 增加 <repositories> <repository> <id>maven-rest ...
- File类最基础知识
package File; /** * 创建一个文件: * 判断是否存在,若存在,则创建,若不存在,则删除,最后输出文件是否存在. */ import java.io.File; import jav ...
- 介绍SmartUpload很好的网站
附带链接:http://www.cnblogs.com/elleniou/archive/2012/09/24/2700583.html
- postgresql行转列并拼接字符串
有这样一张表: ; id | kw ----+-------- 1 | big 1 | hello 2 | oracle 2 | small 2 | apple 3 | shit( ...
- jBPM开发入门指南
http://blog.csdn.net/eric474470/article/details/7665265 工作流虽然还在不成熟的发展阶段,甚至还没有一个公认的规范标准.但其应用却已经在快速展开, ...
- initMethod 和 afterPropertiesSet 以及 AwareMethod方法的执行时机
在spring开发中,我们定义bean 经常会需要用到beanFactory对象,这就需要实现BeanFactoryAware这种类型的接口,它有一个setBeanFactory方法 在xml中配 ...
- ActiveMQ系列(1) - 使用入门
没网的日子真的不好过啊 1.背景: 对于常见业务中,数据并发是一个很头疼的问题,很多时候,会出现资源共享导致线程阻塞的问题,这时候问题就来了,,,老板也尾随来了,来 ...