• XmlDocument.SelectNodes 不起作用


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

    问题分析:

    错误代码如下:

          XmlDocument xmlD = new XmlDocument();
          xmlD.PreserveWhitespace = true;
          xmlD.LoadXml(xStr);
          xmlD.SelectNodes(@"job-scheduling-data/schedule/job");
    
    • 1
    • 2
    • 3
    • 4

    经排查 dotnet 文档,发现代码编写没有问题。文档描述如下:
    在这里插入图片描述

    文档示例如下:
    示例代码:

    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);
          }
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    示例XML

    
    
    <bookstore xmlns:bk="urn:samples">
      <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
        <title>Pride And Prejudicetitle>
        <author>
          <first-name>Janefirst-name>
          <last-name>Austenlast-name>
        author>
        <price>24.95price>
      book>
      <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
        <title>The Handmaid's Taletitle>
        <author>
          <first-name>Margaretfirst-name>
          <last-name>Atwoodlast-name>
        author>
        <price>29.95price>
      book>
      <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
        <title>Emmatitle>
        <author>
          <first-name>Janefirst-name>
          <last-name>Austenlast-name>
        author>
        <price>19.95price>
      book>
      <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
        <title>Sense and Sensibilitytitle>
        <author>
          <first-name>Janefirst-name>
          <last-name>Austenlast-name>
        author>
        <price>19.95price>
      book>
    bookstore>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    自己程序采用Xml:
    在这里插入图片描述

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

    修改后正确代码

         string xStr = File.ReadAllText(path.Trim());
                    xStr = xStr.Replace("", "");
                    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");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    【进击的JavaScript|高薪面试必看】JS基础-作用域和闭包
    uniapp h5实现微信公众号登录
    认识并安装WSL
    使用 Docker 搭建 Hadoop 分布式环境
    [Unity好插件之PlayMaker]PlayMaker如何扩展额外创建更多的脚本
    Leetcode—2609.最长平衡子字符串【简单】
    项目开源!基于PaddleDetection打造实时人体姿态检测的多关节控制皮影机器人
    springboot毕设项目大学生请假系统 fq91k(java+VUE+Mybatis+Maven+Mysql)
    CSS选择器 前端开发入门笔记(十)
    C++ 核心编程(2)
  • 原文地址:https://blog.csdn.net/weixin_46681279/article/details/132583579