如何编辑或重写整个CustomizedFieldCollection
。有2 CustomizedField
。我需要动态编辑CustomizedFiledCollection
。它可以是一个或多个CustomizedField
。 XmlWriter
或XDocument
。
<MainCollection>
<CustomizedFieldCollection>
<CustomizedField>
<Key>Documents Checked</Key>
<DataType>Boolean</DataType>
<Value>false</Value>
</CustomizedField>
<CustomizedField>
<Key>Date Completed</Key>
<DataType>DateTime</DataType>
<Value></Value>
</CustomizedField>
</CustomizedFieldCollection>
</MainCollection>
修改 这是我到目前为止所尝试的。我现在的问题是如何将customizeNode追加到CustomizedFieldCollection。我只是尝试了附加文件。我在youtube中找到了这段代码。这是为集合添加新节点。
XmlDocument doc=new XmlDocument();
doc.Load("C:\\Users\\Reynan\\Desktop\\UDM_TRX_XDC_133.xml");
XmlNode customizedNode = doc.CreateElement("CustomizedField");
XmlNode keyNode = doc.CreateElement("Key");
XmlNode dataNode = doc.CreateElement("DataType");
XmlNode valueNode = doc.CreateElement("Value");
keyNode.InnerText = "QWE";
dataNode.InnerText = "ASD";
valueNode.InnerText = "ZXC";
customizedNode.AppendChild(keyNode);
customizedNode.AppendChild(dataNode);
customizedNode.AppendChild(valueNode);
doc.AppendChild(customizedNode);
doc.Save("C:\\Users\\Reynan\\Desktop\\UDM_TRX_XDC_133.xml");
答案 0 :(得分:1)
我使用以下技术来解析我的XML文件:
XmlDocument xd = new XmlDocument();
xd.Load(filename);
XmlNodeList nodelist = xd.SelectNodes("/CustomizedFieldCollection"); // getFieldCollection
foreach (XmlNode node in nodelist) // foreach Field
{
//Now you just look at the attributes of this XmlNode.
// node.Attributes is useful
//XmlNodeList fields = node.SelectNodes("CustomizedField") might also be fun
// The following pattern might also be useful
// XmlNode xN = db.Attributes.GetNamedItem("Key");
// if (xN != null)
}
答案 1 :(得分:0)
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication59
{
class Program
{
static void Main(string[] args)
{
string xml =
"<CustomizedFieldCollection>" +
"<CustomizedField>" +
"<Key>Documents Checked</Key>" +
"<DataType>Boolean</DataType>" +
"<Value>false</Value>" +
"</CustomizedField>" +
"<CustomizedField>" +
"<Key>Date Completed</Key>" +
"<DataType>DateTime</DataType>" +
"<Value></Value>" +
"</CustomizedField>" +
"</CustomizedFieldCollection>";
XElement element = XElement.Parse(xml);
XElement newField = new XElement("CustomizedField", new XElement[] {
new XElement("key", "Documents Checked"),
new XElement("DataType", "Boolean"),
new XElement("Value", "false")
});
element.Add(newField);
}
}
}