Using Xpath With Default XML Namespace in C#
If you have a XML file without any prefix in the namespace:
<bookstore xmlns="http://www.contoso.com/books">
…
</bookstore>
you have this workaround:
XmlTextReader reader = new XmlTextReader(@"C:\Temp\books.xml");
// ignore the namespace as there is a single default namespace:
reader.Namespaces = false;
XPathDocument document = new XPathDocument(reader);
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator nodes = navigator.Select("//book");
If you have a XML file with a prefix in the namespace:
<bookstore xmlns:ns="http://www.contoso.com/books">
…
</bookstore>
Use this:
XmlTextReader reader = new XmlTextReader(@"C:\Temp\books.xml");
XPathDocument document = new XPathDocument(reader);
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator nodes = navigator.Select("//book");
Of course, you can use a namespace manage if needed:
XmlTextReader reader = new XmlTextReader(@"C:\Temp\books.xml");
XPathDocument document = new XPathDocument(reader);
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(reader.NameTable);
nsmgr.AddNamespace("ns", "http://www.contoso.com/book");
XPathNodeIterator nodes = navigator.Select("//book", nsmgr);
I think that it's the easiest way to make the code working in the most cases.
原文地址:https://stackoverflow.com/questions/585812/using-xpath-with-default-namespace-in-c-sharp/54128951#54128951
Using Xpath With Default XML Namespace in C#的更多相关文章
- XML Namespace (xmlns) 属性
http://www.w3school.com.cn/xml/xml_namespaces.asp XML Namespace (xmlns) 属性 XML 命名空间属性被放置于元素的开始标签之中,并 ...
- Weblogic 9.2 启动时报错 javax.xml.namespace.QName
启动Weblogic 时会报错.javax.xml.namespace.QName; local class incompatible: stream classdesc serialVersionU ...
- The tag 'DataGridTextColumn' does not exist in XML namespace ....
错误 10 The tag 'DataGridTextColumn' does not exist in XML namespace 'http://schemas.microsoft.com/win ...
- Java 异常 —— java.io.InvalidClassException: javax.xml.namespace.QName; local class incompatible
项目中有个 WebService 接口,调试时使用 Main 方法运行,别人的机器上都能运行,就笔者的机器出问题.他们说是RP的问题…… 异常信息: java.io.InvalidClassExcep ...
- java中的xpath,读取xml文档。
1,入门 XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言. XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力.起初 X ...
- struts2 default.xml详解
struts2 default.xml 内容 1 bean节点制定Struts在运行的时候创建的对象类型. 2 指定Struts-default 包 用户写的package(struts.xml) ...
- 使用XPath对象解析xml文件
使用XPath对象解析xml文件 1.DocumentBuilderFactory类 工厂API,使应用程序能从XML文档获取生成DOM对象树的解析器 其构造方法受保护,用newInstance() ...
- 解决 weblogic poi3.9 报错 a different type with name "javax/xml/namespace/QName"
解决 java.lang.LinkageError: loader constraint violation: loader (instance of weblogic/utils/classload ...
- PowerShell技巧:使用XPath语法查询XML文件
[TechTarget中国原创] XML是存储结构化数据的一个很好的途径,但是想要让数据在其中发挥作用又会有些困难.每一种语言都有其特定方式来查询XML文件中的命名空间.元素及属性.PowerShel ...
随机推荐
- java继承-重写-super实例补充
方法重写: 是指子类根据需要父类继承来的方法进行改写,是多态机制的前奏. 重写注意点: 1.重写方法必须和被重写方法具有相同的方法名,参数列表和返回值. 2.重写方法方法不能使用比被重写方法更严格的访 ...
- JQuery的getJSON函数跨域
由于一开始看到“$”等类似符号就头晕,所以注定与PHP和JQuery无缘了,不过自己用JavaScript可不代表其他人也得用,这不,麻烦到了... 两个网站:A.B A站点提供了一个重要的API,由 ...
- 【RabbitMQ】2、RabbitMQ入门程序——Hello World
首先说一下,MQ全称为Message Queue消息队列是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的是程序 ...
- JavaScript事件的委派与事件的绑定
事件的委派 在很多需求中,通常元素是动态创建添加到一个父元素中的,这时候我们点击新增的元素是没有反应的 <script type="text/javascript"> ...
- The Struts dispatcher cannot be found. This is usually caused by using Struts
对于struts2中的问题: org.apache.jasper.JasperException: The Struts dispatcher cannot be found. This is usu ...
- 1053. [HAOI2007]反素数ant【DFS+结论】
Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正整数x满足:g(x)>g(i) 0<i<x ,则称x为反质数.例如,整数 ...
- 3、Android-全局大喇叭-广播机制
所谓的官博机制可以理解成为1对多的概念 即一个喇叭所有的人都能听到(统一范围内) 为了便于及逆行系统级别的消息通知 Android引入了一套广播机制 而且更容易进行实现. 3.1.广播机制的简介 再A ...
- WEB安全 asp+access注入
asp+access注入 数据库 (access数据库没有数据库名) 表名 字段(列名) 记录(行,内容) 注入常用函数: top n 表示查询结果的第n个记录 len() 函数返回文本字段中值的长度 ...
- windows系统安装erlang
1.什么是erlang erlang是一种通用的面向并发的编程语言,它由瑞典电信设备制造商爱立信所辖的CS-Lab开发,目的是创造一种可以应对大规模并发活动的编程语言和运行环境.Erlang是一个结构 ...
- Eclipse常用快捷键(持续更新)
编辑相关快捷键 Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1.[Ctrl+O] 显示类中方法和属性的大纲, ...