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

问题描述 投票:0回答:1

基于此帖子,我想从一些小processing instruction块中读取well-formed XML:>

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

How to read processing instruction from an XML file using .NET 3.5

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

哪里出错?如何获取/阅读加工说明?

我想根据这篇文章从一些小的格式良好的XML块中读取处理指令:如何使用.NET 3.5从XML文件读取处理指令但是,它不起作用。我...

c# xml parsing
1个回答
-2
投票
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);
© www.soinside.com 2019 - 2024. All rights reserved.