C# 如何使用.NET3.5从XML文件读取处理指令

C# 如何使用.NET3.5从XML文件读取处理指令,c#,xml,.net-3.5,processing-instruction,C#,Xml,.net 3.5,Processing Instruction,如何检查Xml文件是否有处理指令 <?xml-stylesheet type="text/xsl" href="Sample.xsl"?> 范例 <?xml-stylesheet type="text/xsl" href="Sample.xsl"?> <Root> <Child/> </Root> 我需要阅读处理说明 <?xml-stylesheet type="text/xsl" href="Sample

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

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

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

 <Root>
    <Child/>
 </Root>

我需要阅读处理说明

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

从XML文件


请帮助我完成此操作。

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

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);
}
分析
数据
属性以获得适当的值。

如何:

XmlProcessingInstruction instruction = doc.SelectSingleNode("processing-instruction('xml-stylesheet')") as XmlProcessingInstruction;

让编译器为您做更多的工作怎么样:

XmlDocument Doc = new XmlDocument();
Doc.Load(openFileDialog1.FileName);

XmlProcessingInstruction StyleReference = 
    Doc.OfType<XmlProcessingInstruction>().Where(x => x.Name == "xml-stylesheet").FirstOrDefault();
XmlDocument Doc=新的XmlDocument();
Doc.Load(openFileDialog1.FileName);
XmlProcessingInstruction StyleReference=
Doc.OfType().Where(x=>x.Name==“xml样式表”).FirstOrDefault();

没有“C#3.5”这样的东西。您询问的是.NET3.5。欢迎使用堆栈溢出!请您的答案解释此答案中的代码是如何工作的。