[英]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
等):
XmlNode
(or a subclass), parent
, representing the parent element of the node you want to replace. 获取XmlNode
(或子类)的实例parent
,表示要替换的节点的父元素。 parent.RemoveChild
to remove the old node. 使用parent.RemoveChild
删除旧节点。 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.InsertAfter
或parent.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.