【问题标题】:Reading XML comments in C#在 C# 中读取 XML 注释
【发布时间】:2011-06-08 11:23:35
【问题描述】:

我有一些在节点上方包含 cmets 的 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>

【问题讨论】:

  • 重要的缺失细节:您用来“读取文件”的代码是什么?
  • 嗯..你怎么看这个
  • 检查this answer

标签: c# xml


【解决方案1】:

试试这个:

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

读取 cmets:

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

【讨论】:

  • false 是 IgnoreComments 的默认值
  • @V4vend:即便如此,在这个答案和实际代码中,明确设置它是一个好主意。
  • 辛苦了,我从来没有意识到节点有这样的类型。
  • 什么是注释放在根元素之外,在本例中是 ?可以读取和解析吗?
【解决方案2】:

使用 System.Xml.Linq:

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

foreach (XComment comment in comments)
   ...

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

      也许它对某人有帮助

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

          一些关于如何访问 cmets 的示例代码希望对您有所帮助

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-12-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-05-19
            • 2015-09-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多