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>
An XML comment encountered outside the document type declaration is represented by the Comment value syntax element. It contains the comment text from the XML message. If the value of the element contains the character sequence --> , the sequence is replaced with the text --> .
The syntax for adding XML comments in your code is triple slashes /// followed by one of the supported XML tags.
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With