XmlDocument.SelectNodes 不起作用

今天采用Xpath读取Xml节点,怎么都读不出。

问题分析:

错误代码如下:

csharp 复制代码
      XmlDocument xmlD = new XmlDocument();
      xmlD.PreserveWhitespace = true;
      xmlD.LoadXml(xStr);
      xmlD.SelectNodes(@"job-scheduling-data/schedule/job");

经排查 dotnet 文档,发现代码编写没有问题。文档描述如下:

文档示例如下:

示例代码:

csharp 复制代码
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

      XmlDocument doc = new XmlDocument();
      doc.Load("booksort.xml");

      //Create an XmlNamespaceManager for resolving namespaces.
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
      nsmgr.AddNamespace("bk", "urn:samples");

      //Select and display the value of all the ISBN attributes.
      XmlNodeList nodeList;
      XmlElement root = doc.DocumentElement;
      nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr);
      foreach (XmlNode isbn in nodeList){
        Console.WriteLine(isbn.Value);
      }
   }
}

示例XML:

xml 复制代码
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

自己程序采用Xml:

结论:问题原因:最后用文档示例与自己代码比较发现上命名空间导致**

修改后正确代码

csharp 复制代码
     string xStr = File.ReadAllText(path.Trim());
                xStr = xStr.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
                xStr = xStr.Replace("xmlns=\"http://quartznet.sourceforge.net/JobSchedulingData\"", "");
                xStr = xStr.Replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
                XmlDocument xmlD = new XmlDocument();
                xmlD.PreserveWhitespace = true;
                xmlD.LoadXml(xStr);
                XmlNodeList jobNodeList = xmlD.SelectNodes(@"job-scheduling-data/schedule/job");
相关推荐
BIGFISH20199 小时前
上下相机引导贴合的标定(绝对坐标方式)
c#
燃尽了,可无13 小时前
C#基础编程核心知识点总结
开发语言·c#
我不是程序猿儿15 小时前
【C#】观察者模式 + UI 线程调度、委托讲解
观察者模式·ui·c#
专注VB编程开发20年15 小时前
c# .net支持 NativeAOT 或 Trimming 的库是什么原理
前端·javascript·c#·.net
钢铁男儿16 小时前
C# 简单工厂模式(简单工厂模式如何工作)
前端·c#·简单工厂模式
isyoungboy18 小时前
c#实现鼠标mousemove事件抽稀,避免大数据阻塞网络
c#·计算机外设·远程桌面·deskflow
一枚小小程序员哈1 天前
基于asp.net 的在线餐饮订餐系统的设计与实现/基于c#的网上订餐系统/餐厅管理系统
后端·c#·asp.net
好望角雾眠1 天前
第三阶段数据库-7:sql中函数,运算符,常用关键字
数据库·笔记·sql·学习·sqlserver·c#
海绵宝宝汉堡包1 天前
c# 项目 文件夹
开发语言·c#
曹牧1 天前
C#:窗体间传值
c#