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. Jackson实现Object对象与Json字符串的互转

    在项目开发过程中,当客户端与服务器响应时,数据交互是必不可少的.然而通过Json实现数据交互成为我们开发中的一部分,进而Jackson为我们的Json转化提供了很好的机制.下面我将利用实例总结如何使用 ...

  2. 数据库连接池及并发库Theron

  3. UVa 1515 - Pool construction(最小割)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. 【[国家集训队]Crash的数字表格 / JZPTAB】

    这道题我们要求的是 \[\sum_{i=1}^N\sum_{j=1}^Mlcm(i,j)\] 总所周知\(lcm\)的性质不如\(gcd\)优雅,但是唯一分解定理告诉我们\(gcd(i,j)\time ...

  5. win10 系统下获取系统版本号为6.2的问题

    近期赶时髦升级了win10,用着挺爽.但是某天在测试一个bug时发现要对win10做特殊处理,于是直接调用了GetVersionEx,并取出版本号进行判断,但是发现得到的版本竟然是6.2.当时就被雷到 ...

  6. ZooKeeper学习之路 (八)ZooKeeper原理解析

    ZooKeeper中的各种角色 ZooKeeper与客户端 每个Server在工作过程中有三种状态: LOOKING:当前Server不知道leader是谁,正在搜寻 LEADING:当前Server ...

  7. XCODE7 和IOS9适配后的一些问题

    网上比较常规的几个问题就不细说了. 什么HTTPS.bitcode.什么什么的. 记录几个自己又遇到了但是网上没有说的. 启动应用报错, *** Terminating app due to unca ...

  8. 如何异步的处理restful服务(基础)

    1.使用Runnable 2.使用DeferredResult 3.异步处理的一些配置 正常请求方式 package com.nxz.controller; import lombok.extern. ...

  9. SharePoint客户端对象模型—任务日历生成

    1,憋了好几天在经理帮助下用Js根据任务列表,生成的个人任务日历. (1)需要用到的CSS样式 <style type="text/css"> th.ms-vh { c ...

  10. Ubuntu install 错误 E:Unable to locate package

    今天在 Ubuntu 上执行 sudo apt install sl 命令,结果报错:E:Unable to locate package sl 上网查询了一下,先更新一下 apt-get,执行:su ...