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#的更多相关文章

  1. XML Namespace (xmlns) 属性

    http://www.w3school.com.cn/xml/xml_namespaces.asp XML Namespace (xmlns) 属性 XML 命名空间属性被放置于元素的开始标签之中,并 ...

  2. Weblogic 9.2 启动时报错 javax.xml.namespace.QName

    启动Weblogic 时会报错.javax.xml.namespace.QName; local class incompatible: stream classdesc serialVersionU ...

  3. The tag 'DataGridTextColumn' does not exist in XML namespace ....

    错误 10 The tag 'DataGridTextColumn' does not exist in XML namespace 'http://schemas.microsoft.com/win ...

  4. Java 异常 —— java.io.InvalidClassException: javax.xml.namespace.QName; local class incompatible

    项目中有个 WebService 接口,调试时使用 Main 方法运行,别人的机器上都能运行,就笔者的机器出问题.他们说是RP的问题…… 异常信息: java.io.InvalidClassExcep ...

  5. java中的xpath,读取xml文档。

    1,入门 XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言. XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力.起初 X ...

  6. struts2 default.xml详解

    struts2  default.xml 内容 1 bean节点制定Struts在运行的时候创建的对象类型. 2 指定Struts-default 包  用户写的package(struts.xml) ...

  7. 使用XPath对象解析xml文件

    使用XPath对象解析xml文件 1.DocumentBuilderFactory类  工厂API,使应用程序能从XML文档获取生成DOM对象树的解析器 其构造方法受保护,用newInstance() ...

  8. 解决 weblogic poi3.9 报错 a different type with name "javax/xml/namespace/QName"

    解决 java.lang.LinkageError: loader constraint violation: loader (instance of weblogic/utils/classload ...

  9. PowerShell技巧:使用XPath语法查询XML文件

    [TechTarget中国原创] XML是存储结构化数据的一个很好的途径,但是想要让数据在其中发挥作用又会有些困难.每一种语言都有其特定方式来查询XML文件中的命名空间.元素及属性.PowerShel ...

随机推荐

  1. angularJs的工具方法2

    一.angular.isArray     判断是否是数组 var a = []; console.log(angular.isArray(a)); //判断参数里面的是否是数组 二.angular. ...

  2. php-fpm配置参数.md

    Global Options pid string PID文件的位置.默认为空.默认路径放在/usr/local/php-fpm/var. error_log string 错误日志的位置.默认:安装 ...

  3. 11GR2 中的常见 RMAN 问题

    版权声明:本文为博主原创文章,欢迎转载验证并评论,谢谢! https://blog.csdn.net/li70803/article/details/34104511 本文是Oracle suppor ...

  4. POJ3690 Constellations

    嘟嘟嘟 哈希 刚开始我一直在想二维哈希,但发现如果还是按行列枚举的话会破坏子矩阵的性质.也就是说,这个哈希只能维护一维的子区间的哈希值. 所以我就开了个二维数组\(has_{i, j}\)表示原矩阵\ ...

  5. luogu P3381【模板】最小费用最大流

    嘟嘟嘟 没错,我开始学费用流了! 做法也是比较朴素的\(spfa\). 就是每一次以费用为权值跑一遍\(spfa\)找到一条最短路,然后把这条道全流满,并把这一次的流量和费用累加到答案上.因此我们需要 ...

  6. 31、springboot与任务

    异步任务 测试如下: 进行等待三秒在进行应答 @Service public class AsynService { public void hello(){ try { Thread.sleep() ...

  7. Selenium封装

    import os from selenium import webdriver from selenium.webdriver.common.by import By from selenium.w ...

  8. Linux API fork 子进程 创建 & 无名管道通信

    #include<unistd.h> #include<stdio.h> int main() { ]; ]; pipe(filedes); printf("my p ...

  9. 如何导出SHP文件中的点坐标?(ArcGIS10)

    行政区域坐标,网上流传较广的版本是包括海域的,假如你仅仅想要把陆地边界绘出,那么怎么办呢? 现在讲一下用arcgis 10从shp线.面文件中获取对应区域的坐标呢?(点图层忽略第一步) 首先用在arc ...

  10. ext4文件系统特性浅析

    ext4作为Linux下的文件系统因其简单性.易管理性.兼容性强等特定,深受大多数用户喜欢,并且作为大多数Linux发行版中的默认文件系统.但是随着现在文件数目的增多以及文件数据的增大,ext4文件系 ...