2011-06-08 267 views
11

我已經得到了一些XML節點上方的註釋文件。當我正在閱讀文件時,作爲該過程的一部分,我希望將評論也發佈出去。我知道你可以使用XmlComment來寫文件的註釋,但不知道如何將它們讀出來。在C#中讀取XML註釋

我的XML看起來類似於此:

<Objects> 
    <!--Comment about node--> 
    <GUID-bf2401c0-ef5e-4d20-9d20-a2451a199362> 
    <info job="SAVE" person="Joe" />  
    <info job="SAVE" person="Sally" />  
    </GUID-bf2401c0-ef5e-4d20-9d20-a2451a199362> 
    <!--Another Comment about node--> 
    <GUID-bf2401c0-ef5e-4d20-9d20-a5844113284112> 
    <info job="SAVE" person="John" />  
    <info job="SAVE" person="Julie" />  
    </GUID-bf2401c0-ef5e-4d20-9d20-a5844113284112> 

+1

重要缺少細節:什麼是您使用的代碼「中讀取文件」? – Tomalak 2011-06-08 11:28:01

+0

嗯..你是怎麼看這 – V4Vendetta 2011-06-08 11:29:43

+0

檢查[這個答案](http://stackoverflow.com/questions/365794/how-do-i-use-xcomment-when-reading-in-an-xml-document/ 365870#365870) – marto 2011-06-08 11:34:28

回答

16

試試這個:

XmlReaderSettings readerSettings = new XmlReaderSettings(); 
readerSettings.IgnoreComments = false; 
using (XmlReader reader = XmlReader.Create("input.xml", readerSettings)) 
{ 
    XmlDocument myData = new XmlDocument(); 
    myData.Load(reader); 
    // etc... 
} 

要閱讀評論:

XmlReader xmlRdr = XmlReader.Create("Test.XML"); 
// Parse the file 
while (xmlRdr.Read()) 
{ 
    switch (xmlRdr.NodeType) 
    { 
     case XmlNodeType.Element: 
      // You may need to capture the last element to provide a context 
      // for any comments you come across... so copy xmlRdr.Name, etc. 
      break; 
     case XmlNodeType.Comment: 
      // Do something with xmlRdr.value 
+2

false是'IgnoreComments'的默認值 – V4Vendetta 2011-06-08 11:33:38

+2

@ V4vend:即使如此,無論是在這個答案中還是在真實代碼中,明確設置它都是一個好主意。 – 2011-06-08 11:39:06

+0

Right說@Henk Holterman – 2011-06-08 11:40:06

0

我把你的XML存儲到一個文件中,這裏是代碼示例。

 XmlDocument document = new XmlDocument(); 
     document.Load("test.xml"); 
     foreach (XmlComment comment in document.SelectNodes("//comment()")) 
     { 
      Console.WriteLine("Comment: \"{0}\".", comment.Value); 
     } 
10

使用System.Xml.Linq的:昨天

var doc = XElement.Load(fileName); 
var comments = doc.DescendantNodes().OfType<XComment>(); 

foreach (XComment comment in comments) 
    ... 
0

如何進入評論希望這有助於

using System; 
using System.IO; 
using System.Xml; 

public class Sample { 

    public static void Main() { 

    XmlDocument doc = new XmlDocument(); 
    doc.LoadXml(@"<Objects><!--Comment about node--><othernode/><!--Some more comment--></Objects>"); 

    XmlNode root = doc.FirstChild; 
    if (root.HasChildNodes) 
    { 
     for (int i=0; i<root.ChildNodes.Count; i++) 
     { 
     if( root.ChildNodes[i] is XmlComment) 
      Console.WriteLine(root.ChildNodes[i].InnerText); 
     } 
    } 
    } 
} 
1

我知道這個問題已經很老了一些示例代碼,但我有同樣的問題。因此,這裏是我的解決方案:

XmlReaderSettings settings = new XmlReaderSettings(); 
settings.IgnoreWhitespace = false; 
settings.IgnoreComments = false; 
XmlReaderSettings settings2 = new XmlReaderSettings(); 
settings2.IgnoreWhitespace = false; 
settings2.IgnoreComments = false; 
XmlReader xmlreaderOriginalCfg = XmlReader.Create(@"C:\...xml", settings); 
XmlReader xmlreaderVerificationCfg = XmlReader.Create(@"C:\....xml", settings); 

XmlDocument myData = new XmlDocument(); 
myData.Load(xmlreaderOriginalCfg); 
XmlDocument myData2 = new XmlDocument(); 
myData2.Load(xmlreaderVerificationCfg); 

XmlNode parentNode = myData.SelectSingleNode("/configuration/appSettings"); 

foreach (XmlComment comment in myData2.SelectNodes("//comment()")) 
{ 
    XmlComment importedCom = myData.CreateComment(comment.Value); 
    parentNode.AppendChild(importedCom); 

    foreach (XmlNode node in myData2.DocumentElement.SelectNodes("/configuration/appSettings/add")) 
    { 
      XmlNode imported = myData.ImportNode(node, true); 
      parentNode.AppendChild(imported); 
    } 
} 
myData.Save(this.pathNew); 

也許它可以幫助別人