2010-06-23 48 views
4

如何檢查XML文件是否具有處理指令如何使用.NET 3.5

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

<Root> 
    <Child/> 
</Root> 

我需要閱讀的處理指令

<?xml-stylesheet type="text/xsl" href="Sample.xsl"?> 
來讀取XML文件處理指令

來自XML文件。

請幫我做到這一點。

+1

有沒有這樣的事情 「C#3.5」。你在問關於.NET 3.5。 – 2010-06-23 09:50:39

回答

16

如何:

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

可以使用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); 
} 

解析ValueData屬性來獲取相應的值。

0

如何讓編譯器爲你做更多的工作:

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

XmlProcessingInstruction StyleReference = 
    Doc.OfType<XmlProcessingInstruction>().Where(x => x.Name == "xml-stylesheet").FirstOrDefault(); 
+0

歡迎來到堆棧溢出!請[編輯]你的答案來解釋這個答案中的代碼是如何工作的。 – dorukayhan 2016-09-23 20:04:13