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

1

6

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

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

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>
Consecrate answered 9/9, 2010 at 13:32 Comment(0)
C
9

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

  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

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.

  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);
  }
Cavicorn answered 9/9, 2010 at 13:41 Comment(4)
how do I get this particular commented node . (There could be several commented nodes). Is there a way to use XQuery for this ?Consecrate
@Anath, I guess the best option would be to use XPath query XmlDocument.SelectNodes() to get the comments and then check the string for the pattern you are interested in, using either string.IndexOf or if the pattern is more complex you can use a RegEx.Cavicorn
@Anath, I added an example to address your comment above. Does this help?Cavicorn
It adds xmlns="" in node. How can i add namespace reference in code so that the attribute does not append?Valence

© 2022 - 2024 — McMap. All rights reserved.