我是C#和XML的新手,我需要以下代码的帮助,我已经通过C#代码创建了一个Element,并希望在两个地方的XML文件中插入City Element并编码以下代码。但是C#代码只在第4个节点中插入Element,而不是在第1个节点,我无法弄清楚我错过了什么。任何帮助都将不胜感激。如果我只保留一个INSERTAFTER,它工作正常。
XmlElement childElement = doc.CreateElement("City"); // Creation of <City> ....... <City>
childElement.InnerText = "Hyderabad";// Adding Value <City> Hyderabad </City>
XmlNode SelectNode = doc.SelectSingleNode("Group/user"); //where to add XPATH expression
SelectNode.InsertAfter(childElement, SelectNode.LastChild);//selects 1st "user" node lastchild and insert after
XmlNode refNode = doc.SelectSingleNode("Group/user[4]");// Indicating 4 Node in XML file
refNode.InsertAfter(childElement, refNode.LastChild); //selects 4th "user" node lastchild and insert after
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<Group>
<user>
<Name age="39">John Hay</Name>
<RollNo>01</RollNo>
=== <City>Hyderabad</City> ===> is missing
</user>
<user>
<Name age="11">Ramsey</Name>
<RollNo>02 </RollNo>
</user>
<user>
<Name age="16">Roshan</Name>
<RollNo>03</RollNo>
</user>
<user>
<Name age="42">Rahiman</Name>
<RollNo>04</RollNo>
<City>Hyderabad</City> ==> This is fine.
</user>
</Group>
我可以根据Text.value引用任何ELEMENT(例如RollNo)并再插入一个元素吗?
谢谢
答案 0 :(得分:1)
您正在围绕xml文档移动城市节点的相同实例。你需要实例化另一个:
XmlElement childElement = doc.CreateElement("City"); // Creation of <City> ....... <City>
childElement.InnerText = "Hyderabad";// Adding Value <City> Hyderabad </City>
XmlNode SelectNode = doc.SelectSingleNode("Group/user"); //where to add XPATH expression
SelectNode.InsertAfter(childElement, SelectNode.LastChild);//selects 1st "user" node lastchild and insert after
childElement = doc.CreateElement("City"); // Creation of <City> ....... <City>
childElement.InnerText = "Hyderabad";// Adding Value <City> Hyderabad </City>
XmlNode refNode = doc.SelectSingleNode("Group/user[4]");// Indicating 4 Node in XML file
refNode.InsertAfter(childElement, refNode.LastChild); //selects 4th "user" node lastchild and insert after
/编辑:克隆你的城市节点可能更合适一些。 https://msdn.microsoft.com/de-de/library/system.xml.xmlelement.clonenode(v=vs.110).aspx
XmlElement childElement = doc.CreateElement("City"); // Creation of <City> ....... <City>
childElement.InnerText = "Hyderabad";// Adding Value <City> Hyderabad </City>
XmlNode SelectNode = doc.SelectSingleNode("Group/user"); //where to add XPATH expression
SelectNode.InsertAfter(childElement, SelectNode.LastChild);//selects 1st "user" node lastchild and insert after
XmlNode refNode = doc.SelectSingleNode("Group/user[4]");// Indicating 4 Node in XML file
refNode.InsertAfter(childElement.CloneNode(true), refNode.LastChild); //selects 4th "user" node lastchild and insert after
答案 1 :(得分:0)
您可以使用XPath中的text()
轻松访问特定的nodex ...
看一下这个例子:
//get roll node by it's text
var rollNode = doc.SelectSingleNode("//user/RollNo[text()='03']");
//user to whom roll is child
var parentUserNode = rollNode.ParentNode;
//add some nodex
parentUserNode.InsertAfter(childElement, parentUserNode.LastChild);
答案 2 :(得分:0)
使用LINQ to XML API而不是旧的XmlDocument
API会更容易。例如,要按顺序执行此操作:
var doc = XDocument.Parse(xml);
var hyderabad = new XElement("City", "Hyderabad");
doc.Descendants("user").First().Add(hyderabad);
doc.Descendants("user").Skip(3).First().Add(hyderabad);
或者按RollNo
选择:
var doc = XDocument.Parse(xml);
var roll4 = doc.Descendants("user").Single(x => (int) x.Element("RollNo") == 4);
roll4.Add(new XElement("City", "Hyderabad"));
请参阅this fiddle了解演示。