Linux 使用 C# 取消注释 XML 文件中的注释节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:08:43  来源:igfitidea点击:

To uncomment a commented node in a XML file using C#

c#xmlxmldom

提问by Ananth

I have a XML file which has a node which is commented out. I have created this node using the syntax:

我有一个 XML 文件,其中有一个被注释掉的节点。我使用以下语法创建了这个节点:

relTableCommentedNode = xDoc.CreateNode(XmlNodeType.Comment, "RELTABLECOMMENTED", "np");

relTableCommentedNode = xDoc.CreateNode(XmlNodeType.Comment, "RELTABLECOMMENTED", "np");

What is the best approach to uncomment this node? Can I identify this node based on the name which I used to create the node (RELTABLECOMMENTED)?

取消注释此节点的最佳方法是什么?我可以根据我用来创建节点的名称 ( RELTABLECOMMENTED)来识别这个节点吗?

This the commented node:

这是注释节点:

<code>
<pre> 
<!--<reltable toc="no" class="- map/reltable ">
    <relheader class="- map/relheader ">
      <relcolspec type="concept" class="- map/relcolspec ">      
    </relheader>
    <relrow class="- map/relrow ">
      <relcell class="- map/relcell ">
        <topicref href="concepts\about_cannedgoods.dita" copy-to="concepts\about_cannedgoods.dita" class="- map/topicref " xmlns:dctm="http://www.documentum.com">
        </topicref>
      </relcell>      
    </relrow>
  </reltable> -->
</pre>
</code>

采纳答案by Chris Taylor

To the best of my knowledge, using XmlDocument, there is no direct way to do this. You will need to do something like the following

据我所知,使用 XmlDocument,没有直接的方法可以做到这一点。您将需要执行以下操作

  1. Get the value of the comment node
  2. Create a new XmlNode with the value from step 1
  3. Delete the comment node
  4. Add the new node from step 2 to the DOM tree
  1. 获取评论节点的值
  2. 使用步骤 1 中的值创建一个新的 XmlNode
  3. 删除评论节点
  4. 将步骤 2 中的新节点添加到 DOM 树中

Here is an example with a slightly simplified version of your XML and addressing your quesion in the comments on finding the correct comment node. Note that I query for all comment nodes, obviously you can be more specific and query the portion of the document that you are interested in.

这是一个示例,其中包含稍微简化的 XML 版本,并在有关查找正确注释节点的注释中解决了您的问题。请注意,我查询了所有评论节点,显然您可以更具体地查询您感兴趣的文档部分。

  string xml = @"
    <root>
      <!--<reltable toc='no' class='- map/reltable '>
      <relheader class='- map/relheader '>
        <relcolspec type='concept' class='- map/relcolspec '/>      
      </relheader>         
    </reltable> -->

    <!--<reltable toc='no' class='- map '>
      <relheader class='- map/relheader '>
        <relcolspec type='concept' class='- map/relcolspec '/>      
      </relheader>          
    </reltable> -->
  </root>";

  XmlDocument xdoc = new XmlDocument();
  xdoc.LoadXml(xml);

  XmlNodeList commentedNodes = xdoc.SelectNodes("//comment()");
  var commentNode = (from comment in commentedNodes.Cast<XmlNode>()
              where comment.Value.Contains("class='- map '")
              select comment).FirstOrDefault();

  if (commentNode != null)
  {
    XmlReader nodeReader = XmlReader.Create(new StringReader(commentNode.Value));
    XmlNode newNode = xdoc.ReadNode(nodeReader);
    commentNode.ParentNode.ReplaceChild(newNode, commentNode);
  }