使用WPF / C#取消注释XML节点 [英] Uncomment XML node using WPF/C#

查看:94
本文介绍了使用WPF / C#取消注释XML节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的xml文件:



this my xml file :

<?xml version="1.0" encoding="utf-8"?>

<Sequence>
  <Inputs>

<!--<Input>readOF</Input> -->
    <Input>readReference</Input>
   
  </Inputs>
 </Step>-->

  </Steps></Sequence>







我想取消注释这一行:




I want to uncomment this line :

<!--<Input>readOF</Input> -->





所需输出:



Desired output:

<Input>readOF</Input>





我尝试过:



当我试图评论xml节点时,这是我的代码:





What I have tried:

this is my code when I tried to comment xml node :

var document = new XmlDocument();
document.LoadXml(text);
XmlNodeList nodes = document.SelectNodes("/Sequence/Inputs/Input[.='readOF']");
foreach (XmlNode node in nodes)
  node.ParentNode.ReplaceChild(document.CreateComment(node.OuterXml), node);





完美无缺。

但现在我想要的是反过来:取消注释特定的xml节点。

感谢您的帮助:)



It works perfectly .
But now what I want is the reverse : uncomment that specific xml node .
Thank you for any help :)

推荐答案

看看:使用C#取消注释XML文件中的注释节点,由回答克里斯泰勒







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



1.获取注释节点的值

2.创建一个新的XmlNode步骤1中的值

3.删除注释节点

4.将步骤2中的新节点添加到DOM树



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

Take a look: To uncomment a commented node in a XML file using C# answered 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

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


看起来你正试图扭转你在先前问题中所做的事情。如果是这样,我会做类似以下的事情:



It looks like you're trying to reverse what you've done in a prior question. If so, I'd do something like the following:

XmlNodeList nodes = document2.SelectNodes("/Sequence/Inputs/comment()[starts-with(., '<Input>')]");
foreach (XmlNode node in nodes)
{
  XmlDocumentFragment fragment = document2.CreateDocumentFragment();
  fragment.InnerXml = node.Value;

  var element = fragment.FirstChild as XmlElement;
  if (element.FirstChild.Value == "readOF")
    node.ParentNode.ReplaceChild(element, node);
}



如需参考(其他人),可以在以下网址找到先前的问题:



注释特定输入XML文件代码,使用WPF。 [ ^ ]



其他一些要查看的链接:



XPath语法 [ ^ ]



基本查询(LINQ to XML)(C#)| Microsoft Docs [ ^ ]


For reference (for others), prior question can be found at:

Comment specific input XML file code , using WPF .[^]

Some other links to check out:

XPath Syntax[^]

Basic Queries (LINQ to XML) (C#) | Microsoft Docs[^]


这篇关于使用WPF / C#取消注释XML节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆