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");
相关推荐
薄荷撞~可乐7 小时前
C#Task(Api)应用
开发语言·c#
almighty2712 小时前
C#WPF控制USB摄像头参数:曝光、白平衡等高级设置完全指南
开发语言·c#·wpf·usb相机·参数设置
后青春期的诗go13 小时前
金蝶云星空插件开发记录(一)
c#·钉钉·金蝶云星空·插件开发
大飞pkz18 小时前
【设计模式】题目小练1
开发语言·设计模式·c#·题目小练
lljss202019 小时前
C# 每个chartArea显示最小值、平均值、最大值
开发语言·c#
wearegogog12320 小时前
C#与Twincat 2 实现上位机控制软PLC功能
开发语言·c#
军训猫猫头20 小时前
12.NModbus4在C#上的部署与使用 C#例子 WPF例子
开发语言·c#·wpf
Eiceblue21 小时前
使用 C# 设置 Excel 单元格格式
开发语言·后端·c#·.net·excel
LostXerxes1 天前
C#的继承和多态
c#
薄荷撞~可乐1 天前
C#高并发与并行理解处理
开发语言·c#