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");
相关推荐
Song_da_da_8 小时前
C#与VisionPro联合编程实战:机器视觉二次开发完整指南
开发语言·microsoft·c#
加号311 小时前
【C#】 Web API 自定义配置函数请求路径:从路由本质到灵活架构设计
开发语言·c#
happyprince13 小时前
11-Hugging Face Transformers 分布式与并行系统深度分析
分布式·c#·wpf
csdn_aspnet15 小时前
C# list集合 多属性排序
c#·list·linq·排序
加号315 小时前
【WPF】 基于 Canvas 读取并渲染 DXF 文件的技术指南
c#·wpf
天下无敌笨笨熊15 小时前
SNMP协议开发心得
网络协议·c#
创可贴治愈心灵16 小时前
AI浪潮下C#就业前景剖析:深耕C#为主,按需选修Java与Python
java·人工智能·c#
专注VB编程开发20年18 小时前
开发VS2026插件最佳方案:老式VSIX EnvDTE
ide·c#·visual studio
专注VB编程开发20年18 小时前
VS2026最新ide插件VisualStudio.Extensibility进程外 OOP 新模型
ide·c#·visual studio
scan72419 小时前
短期记忆记忆存储在内存里,一个会话里的多轮对话
开发语言·c#