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

例子

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

 <Root>
    <Child/>
 </Root>

我需要阅读处理指令

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

从XML文件。

请帮助我这样做。

有帮助吗?

解决方案

怎么样:

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

其他提示

您可以使用 FirstChild 财产的 XmlDocument 班级和 XmlProcessingInstruction 班级:

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);
}

解析 Value 或者 Data 属性以获取适当的值。

让编译器为您做更多的工作如何:

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

XmlProcessingInstruction StyleReference = 
    Doc.OfType<XmlProcessingInstruction>().Where(x => x.Name == "xml-stylesheet").FirstOrDefault();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top