以编程方式注释 xml 元素 [英] Comment xml elements programmatically

查看:27
本文介绍了以编程方式注释 xml 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的 xml 文件,

<预><代码><配置><财产><姓名>姓名</姓名><value>dinesh</value></属性><财产><名称>城市</名称><value>德里</value></属性></配置>

我的要求是我需要在运行时根据属性名称以编程方式注释/取消注释属性,如下所示;

<预><代码><配置><!-- <财产><姓名>姓名</姓名><value>dinesh</value></属性>--><财产><名称>城市</名称><value>德里</value></属性></配置>

是否有任何直接的方法可以通过 XDocument/XmlDocument 遍历来实现?我刚刚从这个问题中浏览了如下代码,

XmlComment DirCom = doc.CreateComment(XmlElementName.OuterXml);doc.InsertAfter(DirCom, XmlElementName);doc.RemoveChild(XmlElementName)

以上代码的使用方法是否正确?

解决方案

你可以用 XDocument

轻松搞定

var xDocument = XDocument.Parse(@"<财产><姓名>姓名</姓名><value>dinesh</value></属性><财产><名称>城市</名称><value>德里</value></属性></配置>");var firstPropertyElement = xDocument.后代(财产").First();//找到你的元素var xComment = new XComment(firstPropertyElement.ToString());//创建评论firstPropertyElement.ReplaceWith(xComment);//用注释替换元素Console.WriteLine(xDocument);

输出:

<预><代码><配置><!--<财产><姓名>姓名</姓名><value>dinesh</value></property>--><财产><名称>城市</名称><value>德里</value></属性></配置>

I have an xml file like below,

<configuration>
    <property>
      <name>name</name>
      <value>dinesh</value>
    </property>
    <property>
      <name>city</name>
      <value>Delhi</value>
    </property>
</configuration>

My requirement is I need to comment / uncomment properties programmatically at runtime based on property's name like below;

<configuration>
    <!-- <property>
      <name>name</name>
      <value>dinesh</value>
    </property> -->
    <property>
      <name>city</name>
      <value>Delhi</value>
    </property>
</configuration>

Is there any direct way to achive this via XDocument / XmlDocument traversal? I just gone through code like below from this question,

XmlComment DirCom = doc.CreateComment(XmlElementName.OuterXml);
doc.InsertAfter(DirCom, XmlElementName);    
doc.RemoveChild(XmlElementName)

Is the above code usage a right approach?

解决方案

You can do it easily with XDocument

var xDocument = XDocument.Parse(@"<configuration>
                                    <property>
                                      <name>name</name>
                                      <value>dinesh</value>
                                    </property>
                                    <property>
                                      <name>city</name>
                                      <value>Delhi</value>
                                    </property>
                                </configuration>");

var firstPropertyElement = xDocument
    .Descendants("property")
    .First();//Find your element
var xComment = new XComment(firstPropertyElement.ToString());//Create comment
firstPropertyElement.ReplaceWith(xComment);//Replace the element with comment 
Console.WriteLine(xDocument);

Which outputs:

<configuration>
  <!--<property>
  <name>name</name>
  <value>dinesh</value>
</property>-->
  <property>
    <name>city</name>
    <value>Delhi</value>
  </property>
</configuration>

这篇关于以编程方式注释 xml 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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