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

早过忘川 提交于 2019-12-14 03:12:16

问题


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>

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.


回答1:


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

  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.

(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.)

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.



来源:https://stackoverflow.com/questions/18714386/how-can-i-replace-the-xml-elements-with-new-elements-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!