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");
相关推荐
m0_748248022 小时前
C++与C#布尔类型深度解析:从语言设计到跨平台互操作
c++·stm32·c#
LeonDL1683 小时前
【通用视觉框架】基于C#+VisionPro开发的视觉框架软件,全套源码,开箱即用
人工智能·c#·visionpro·通用视觉框架·机器视觉框架·视觉框架软件·机器视觉软件
一抓掉一大把4 小时前
RuoYi .net-实现商城秒杀下单(redis,rabbitmq)
redis·mysql·c#·rabbitmq·.net
睡前要喝豆奶粉5 小时前
在.NET Core Web Api中使用阿里云OSS
阿里云·c#·.netcore
缺点内向11 小时前
C#: 高效移动与删除Excel工作表
开发语言·c#·.net·excel
yue00813 小时前
C# 分部类读取学生信息
开发语言·c#
聪明努力的积极向上14 小时前
【C#】事件简单解析
开发语言·c#
qq_124987075315 小时前
基于C#的贵州省黔北地区乡村避暑生活共享平台设计与实现(源码+论文+部署+安装)
c#·毕业设计·asp.net·生活
LateFrames20 小时前
C# 中,0.1 在什么情况下不等于 0.1 ?
开发语言·c#
萝卜白菜。1 天前
关于Java EE应用中xml解析类的问题
xml·java·java-ee