[英]Using C# to add an XML node to an exiting XML file
I have an XML document that I am trying to modify... 我有一个要修改的XML文档...
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="contact">
<attribute name="fullname" />
<attribute name="telephone1" />
<attribute name="contactid" />
<order attribute="fullname" descending="false" />
<filter type = "and">
<condition attribute="parentcustomerid" operator="eq" uiname="Tardis Communications" uitype="account" value="{BB0D0E64-C85E-E411-9405-00155D1DEA05}" />
</filter>
</entity>
</fetch>
What I am trying to do is insert this... 我想做的是插入此...
<filter type = "and">
<condition attribute="ownerid" operator= "eq-userid"/>
</filter>
in between the already existing "filter" tags. 在已经存在的“过滤器”标签之间。 the new filter code is from another file (.txt).
新的过滤器代码来自另一个文件(.txt)。
I realize that this may not make sense, however, I just want to see if it is possible. 我意识到这可能没有道理,但是,我只想看看是否有可能。 If so, I can move things around after.
如果是这样,我以后可以四处走动。
Here is what I tried. 这是我尝试过的。
private void button1_Click(object sender, EventArgs e)
{
XmlDataDocument doc = new XmlDataDocument();
doc.Load(@"C:\Users\jellsworth\Downloads\mySampleXML.xml");
//XmlNode node = null;
foreach (XmlNode node in doc.SelectNodes("//filter/condition"))
{
XmlElement mapNode = doc.CreateElement("filter");
XmlAttribute newFilter = doc.CreateAttribute("lattitude");
newFilter.Value = @"C:\Users\jellsworth\Downloads\playFilter.txt";
mapNode.SetAttributeNode(newFilter);
node.InsertBefore(mapNode, node.FirstChild);
}
}
Any guidance would be greatly appreciated. 任何指导将不胜感激。
Try using XDocument. 尝试使用XDocument。
// Load document
XDocument myDoc = XDocument.Load(".\\Main.xml");
// Select child element "entity" then select the child element of it you want which is "filter"
XElement filterNode = myDoc.Root.Element("entity").Element("filter");
//Example to iterate through all of the child nodes with the name condition
foreach (var childNode in filterNode.Descendants("condition")) {
// you could add another attribute to each of them
childNode.SetAttributeValue("", "");
}
// Example element to add
XElement newCondition = new XElement("condition");
newCondition.SetAttributeValue("attribute", "parentcustomerid");
newCondition.SetAttributeValue("operator", "eq");
filterNode.Add(newCondition);
myDoc.Save(".\\newFile.xml");
Basically, load your document with the file path as a string in 基本上,使用文件路径作为字符串加载文档
XDocument.Load("<pathToFile>");
Selecting elements and drilling down is as simple as setting a new XElement myElement = myDoc.root.Element("<Child element name>");
选择元素并向下钻取就像设置一个新的XElement一样简单
myElement = myDoc.root.Element("<Child element name>");
Now myElement will always represent that node and can be iterated through as well. 现在, myElement将始终代表该节点,并且也可以对其进行迭代。 To add a node, just call whatever element such as
要添加节点,只需调用诸如
myElement.Add(<new XElement with attributes set>);
Let me know if you need any more help with another part of it, I'd be happy to help! 如果您需要其他方面的帮助,请告诉我,我们很乐意为您提供帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.