如何检查一个xml文件是否有处理指令
例子
<?xml-stylesheet type="text/xsl" href="Sample.xsl"?>
<Root>
<Child/>
</Root>
我需要阅读处理说明
<?xml-stylesheet type="text/xsl" href="Sample.xsl"?>
从 XML 文件。
请帮助我做到这一点。
如何检查一个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();