如何使用.NET 3.5从XML文件中读取处理指令
如何检查Xml文件是否有处理指令
例
我需要阅读处理指令
来自XML文件。
请帮我这样做。
怎么样:
XmlProcessingInstruction instruction = doc.SelectSingleNode("processing-instruction('xml-stylesheet')") as XmlProcessingInstruction;
您可以使用XmlDocument
类的FirstChild
属性和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().Where(x => x.Name == "xml-stylesheet").FirstOrDefault();