如何使用C#.NET从XML字符串读取处理指令?

时间:2020-05-21 19:34:09

标签: c# xml parsing

基于这篇文章,我想从一些小processing instruction中读取一个well-formed XMLHow to read processing instruction from an XML file using .NET 3.5

但是,它不起作用。我得到的错误是pi对象为null。 这就是我要做的:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>Text <?pi 1?>blabla</root>");
Console.WriteLine(doc.ChildNodes[1].Name); // output: root

XmlProcessingInstruction pi = doc.OfType<XmlProcessingInstruction>().Where(x => x.Name == "pi").FirstOrDefault();
Console.WriteLine(pi.Value);

解析XML是可行的。当我在Visual Studio中收到错误(System-NullReferenceException)时,我在“ Console.WriteLine(pi.Value);”行得到了它。

错误在哪里?如何获取/阅读处理说明?

4 个答案:

答案 0 :(得分:4)

您的代码中有两个问题。

第一个是它在 Console.WriteLine(doc.ChildNodes[1].Name); 上失败,“root”是第一个节点,而不是第二个(从零开始)。但这可能是一个错字。

关于问题:

Enumerable.OfType() 正在使用 XmlDocument 类的 GetEnumerator(),该类由其父类 XmlNode.GetEnumerator() 实现。文档指出:

<块引用>

一个 IEnumerator 对象,可用于遍历当前节点中的子节点。

但是您试图在 doc 上运行 OfType,他只有一个孩子:root

如果您将代码更改为在 root 的子代上运行,它将按预期工作:

XmlProcessingInstruction pi = doc.ChildNodes[0].OfType<XmlProcessingInstruction>().Where(x => x.Name == "pi").FirstOrDefault();

答案 1 :(得分:0)

使用 XmlDocument:

var doc = new XmlDocument();
doc.Load("test.xml");

// get all instructions
var instructions = doc.SelectNodes("//processing-instruction()");

foreach (XmlProcessingInstruction instruction in instructions)
    Console.WriteLine(instruction.Target + " : " + instruction.Data);

// get concrete instruction
var pi = doc.SelectSingleNode("//processing-instruction('pi')") as XmlProcessingInstruction;
Console.WriteLine(pi.Target + " : " + pi.Data);

LINQ 到 XML。它更方便、更容易:

var xml = XDocument.Load("test.xml");

var instructions = xml.DescendantNodes()
    .OfType<XProcessingInstruction>();

foreach (var instruction in instructions)
    Console.WriteLine(instruction);

var pi = instructions.FirstOrDefault(x => x.Target == "pi");
Console.WriteLine(pi.Data);

答案 2 :(得分:0)

我能够以这种方式工作:

 static void Main(string[] args)
   {
    XmlDocument doc = new XmlDocument();
    doc.Load("c:\\test\\xmlData.xml");
             
   List<XmlProcessingInstruction> piList = new List<XmlProcessingInstruction>(); 
      foreach (XmlNode cnode in doc.ChildNodes)
       {
         foreach (var ccnode in cnode)
         {
           if (ccnode is XmlProcessingInstruction)
               piList.Add(ccnode as XmlProcessingInstruction);
         }
      }
      Console.WriteLine(piList.FirstOrDefault(a => a.Name == "pi").Value);
   }

xmlData.xml

   <?xml version="1.0" encoding="utf-8"?>
     <root>
     <?pi 1?>
       <value>6</value>
     </root>

我相信您无法获得 pi 的原因是您的 Pi 目标和值周围没有引号。我无法让它作为字符串工作,但它可以很好地从文件中读取。

答案 3 :(得分:-2)

{
    private float _length;
    private int _toeCount;


    public Socks()
    { //code here;
    }

    public override bool Equals(object obj)
    {
        if (!(obj is Socks arg)) return false;

        return (this._length.Equals(arg._length));
    }

    public override int GetHashCode()
    {
        return 1;
    }

    public static int SortByWidthToe(Socks a, Socks b)
    {
        if (a == null && b == null) return 0;
        if (a == null) return -1;
        if (b == null) return 1;

        if (a._length.Equals(b._length))
            return a._toeCount.CompareTo(b._toeCount);

        return a._length.CompareTo(b._length);
    }
}
相关问题