在C#中读取XML注释

时间:2011-06-08 11:23:35

标签: c# xml

我有一些XML文件,其中包含节点上方的注释。当我在阅读文件时,作为整个过程的一部分,我也希望得到评论。我知道你可以使用 XmlComment 在文件上写评论,但不知道怎么读它们。

我的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>

6 个答案:

答案 0 :(得分: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

答案 1 :(得分:13)

使用System.Xml.Linq:

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

foreach (XComment comment in comments)
   ...

答案 2 :(得分:2)

它们是包含节点的子节点的一部分,与所有其他节点一样:http://msdn.microsoft.com/en-us/library/system.xml.xmlcomment.aspx

答案 3 :(得分:2)

我知道问题很老,但昨天我遇到了同样的问题。所以这是我的解决方案:

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);

也许对某人有帮助

答案 4 :(得分:0)

我将您的XML存储到一个文件中,这是代码示例。

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

答案 5 :(得分: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);
      }
    }
  }
}