如何使用.NET 3.5从XML文件中读取处理指令

Tho*_*eld 5 c# xml .net-3.5 processing-instruction

如何检查Xml文件是否有处理指令

 <?xml-stylesheet type="text/xsl" href="Sample.xsl"?>

 <Root>
    <Child/>
 </Root>
Run Code Online (Sandbox Code Playgroud)

我需要阅读处理指令

<?xml-stylesheet type="text/xsl" href="Sample.xsl"?>
Run Code Online (Sandbox Code Playgroud)

来自XML文件.

请帮我这样做.

Gar*_*ett 16

怎么样:

XmlProcessingInstruction instruction = doc.SelectSingleNode("processing-instruction('xml-stylesheet')") as XmlProcessingInstruction;
Run Code Online (Sandbox Code Playgroud)


bra*_*her 5

您可以使用类和类的FirstChild属性:XmlDocumentXmlProcessingInstruction

XmlDocument doc = new XmlDocument();
doc.Load("example.xml");

if (doc.FirstChild is XmlProcessingInstruction)
{
    XmlProcessingInstruction processInfo = (XmlProcessingInstruction) doc.FirstChild;
    Console.WriteLine(processInfo.Data);
    Console.WriteLine(processInfo.Name);
    Console.WriteLine(processInfo.Target);
    Console.WriteLine(processInfo.Value);
}
Run Code Online (Sandbox Code Playgroud)

解析ValueData属性以获得适当的值.