我如何用c#中的新元素替换xml元素 [英] How can i replace the xml elements with new elements in c#

查看:41
本文介绍了我如何用c#中的新元素替换xml元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用新元素替换 xml 标记中的现有元素.示例 XML 如下:

I would like to replace the existing elements inthe xml tag with the new ones. Sample XML is as follows:

<Dr.Watson>
<Bugs>
  <Bug Name="Bug.add --> AAAAAAAAAAAA">
    <criteria>
      <includeFilterSets>
        <filterSet>
          <filter>
            <filterName>PRODUCT_NAME</filterName>
            <operator>
              <name>Equals</name>
            </operator>
            <value>Dr.Watson</value>
          </filter>
        </filterSet>
      </includeFilterSets>
      <grouping>
        <groupBy>
          <name>STATUS</name>
        </groupBy>
      </grouping>
      <caseSensitive>false</caseSensitive>
      <entityToSearch>
        <name>BUG</name>
      </entityToSearch>
    </criteria>
  </Bug>
  </Bugs>
  </Dr.Watson>

到目前为止我有代码:

XmlDocument doc = new XmlDocument();
doc.LoadXml(FilePath_EXPRESS_API_InputFile);
XmlNodeList nodelist = doc.SelectNodes("/Dr.Watson/Bugs/Bug");


//create node and add value
//Console.WriteLine(mxpwr.Value);
XmlNode node = doc.CreateNode(XmlNodeType.Element, "grouping", null);
XmlNode node11 = doc.CreateNode(XmlNodeType.Element, "groupBy", null);
XmlNode node12 = doc.CreateNode(XmlNodeType.Element, "name", null);

//Create Title Node
XmlNode Test_11 = doc.CreateElement("grouping");
XmlNode Test_22 = doc.CreateElement("groupBy");
XmlNode Test_44 = doc.CreateElement("name");

//add value for it
Test_11.InnerText = ("");
Test_22.InnerText = ("");
Test_44.InnerText = ("");

//create Url node
//XmlNode Test_445 = doc.CreateElement("sai");
Test_44.InnerText = ("STATE");

//add to parent node
Test_11.AppendChild(Test_22);
Test_22.AppendChild(Test_44);

//add to elements collection
doc.DocumentElement.AppendChild(Test_11);
Test_11.AppendChild(Test_22);
Test_22.AppendChild(Test_44);

请建议并帮助我,因为我是 c# 新手,用于 xml 场景.谢谢.另外,请注意,我不想保存这些编辑,并想使用编辑过的 xml 运行时来执行 API.

Please suggest and help me as i am new to c# for xml scenarios.Thanks.Also, please note that i dont want to save these edits and want to use the edited xml runtime for the execution of APIs.

推荐答案

通过 XML-DOM API 替换节点(XmlDocument 等):

To replace a node via the XML-DOM API (XmlDocument et al):

  1. 获取 XmlNode 的实例(或一个子类),parent,表示要替换的节点的父元素.
  2. 使用parent.RemoveChild 删除旧节点.
  3. 使用parent.AppendChild 添加新节点.
  1. Get an instance of XmlNode (or a subclass), parent, representing the parent element of the node you want to replace.
  2. Use parent.RemoveChild to remove the old node.
  3. Use parent.AppendChild to add the new node.

(作为 #3 的替代方案,请使用 parent.InsertAfterparent.InsertBefore 和对另一个子节点的引用,以将新节点放置在其他现有子节点之间.)

(As an alternative to #3 use parent.InsertAfter or parent.InsertBefore and a reference to another child to place the new node amongst other existing children.)

您在问题中编写的代码似乎是从头开始构建一个新的 XML 文档:为什么要替换节点——只需第一次创建正确的节点即可.要修改现有的 XML 文档,请使用静态 XmlDocument.Load 方法加载和解析现有文档,使用各种搜索和导航方法获取上面#1 中的引用,然后应用上述步骤,最后正常保存.

You code in the question appears to be constructing a new XML document from scratch: why would you want to replace a node—just create the right one first time. To modify an existing XML document use one of the static XmlDocument.Load methods to load and parse the existing document, use the various search and navigation methods to get the reference in #1 above, then apply the above steps, and finally save as normal.

这篇关于我如何用c#中的新元素替换xml元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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