![](/img/trans.png)
[英]How to read a processing instruction from XML string using C# .NET?
[英]How to read processing instruction from an XML file using .NET 3.5
如何检查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;
您可以使用XmlDocument
类和XmlProcessingInstruction
类的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);
}
解析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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.