7

如何检查一个xml文件是否有处理指令

例子

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

 <Root>
    <Child/>
 </Root>

我需要阅读处理说明

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

从 XML 文件。

请帮助我做到这一点。

4

3 回答 3

18

怎么样:

XmlProcessingInstruction instruction = doc.SelectSingleNode("processing-instruction('xml-stylesheet')") as XmlProcessingInstruction;
于 2010-06-23T18:38:58.887 回答
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);
}

解析ValueData属性以获取适当的值。

于 2010-06-23T09:40:59.703 回答
0

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

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

XmlProcessingInstruction StyleReference = 
    Doc.OfType<XmlProcessingInstruction>().Where(x => x.Name == "xml-stylesheet").FirstOrDefault();
于 2016-09-23T19:13:35.183 回答