要使用C#注释一个注释节点XML文件 [英] To uncomment a commented node in a XML file using C#
问题描述
我有具有被注释掉节点的XML文件。我创建使用语法此节点 -
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);
relTableCommentedNode = xDoc.CreateNode(XmlNodeType.Comment, "RELTABLECOMMENTED", "np");
什么是去掉这一节点的最佳方法?我可以找出在此基础上我用来创建节点(RELTABLECOMMENTED)这个名字?
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) ?
本的注释节点
<该节点/ p>
This the commented node
<!--<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> -->
推荐答案
要尽我所知,使用XmlDocument的,有做这个没有直接的方法。您将需要做类似如下
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步
- 价值的新的XMLNode删除注释节点
- 从第2步中添加新节点到DOM树
- Get the value of the comment node
- Create a new XmlNode with the value from step 1
- Delete the comment node
- Add the new node from step 2 to the DOM tree
下面与您的XML和稍微简化版本寻址找到正确的注释节点在评论你的quesion一个例子。请注意,我查询所有注释节点,很明显,你可以更具体和查询你感兴趣的文件的部分。
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);
}
这篇关于要使用C#注释一个注释节点XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!