我有我試圖修改XML文檔...用C#的XML節點添加到一個現有的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>
我所試圖做的是插入此...
<filter type = "and">
<condition attribute="ownerid" operator= "eq-userid"/>
</filter>
在已存在的「過濾器」標籤之間。新的過濾器代碼來自另一個文件(.txt)。
我意識到這可能沒有道理,但是,我只是想看看是否有可能。如果是這樣,我可以在之後移動。
這是我試過的。
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);
}
}
任何指導將不勝感激。
如果您要對XML進行大量修改,則應考慮使用XSLT。 XSLT將XML作爲輸入,並輸出該XML的「已轉換」版本。 – Andersnk 2014-11-03 21:10:16