简体   繁体   English

我如何用C#中的新元素替换xml元素

[英]How can i replace the xml elements with new elements in c#

I would like to replace the existing elements inthe xml tag with the new ones. 我想用新元素替换xml标记中的现有元素。 Sample XML is as follows: XML示例如下:

<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>

Code so far i have : 到目前为止,我有代码:

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

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场景的c#的新手。此外,请注意,我不想保存这些编辑,并且不想使用经过编辑的xml运行时来执行API。

To replace a node via the XML-DOM API ( XmlDocument et al): 通过XML-DOM API替换节点( XmlDocument等):

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

(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.) (作为#3的替代方法,请使用parent.InsertAfterparent.InsertBefore以及对另一个孩子的引用,以将新节点放置在其他现有孩子中。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用C#中的另一个xml元素替换xml元素? - how to replace xml elements with another xml elements in c#? 如何使用c#替换xml文件中的子元素 - How to replace sub elements in xml files using c# 在 C# 7 中,如何从现有元素和新元素创建新的 ValueTuple? - In C# 7, How Can I Make a New ValueTuple from an Existing and New Elements? 如何用绝对值替换数组的元素? C# - How can I replace the elements of an array with absolute values instead? c# 在使用C#解释不包含内容的元素时,如何从XML(包含后代)读取兄弟元素的内容? - How can I read the contents of sibling elements from XML (with descendants) while accounting for elements with no content using C#? 使用命名空间时,如何使用C#处理特定的XML元素? - How can I address specific XML-Elements with C# when using namespaces? C#如何从xml文件中获取所有元素名称 - C# how can I get all elements name from a xml file 如何在C#中使用XMLREADER从XML字符串中读取特定元素 - How can I read specific elements from XML string using XMLREADER in C# 如何在C#中使用子元素反序列化XML? - How do I deserialize XML with child elements in C#? 如何在 C# 中使用 XmlDocument 停止空 XML 元素自关闭? - How can I stop empty XML elements self-closing using XmlDocument in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM