您可以使用System.XML命名空間來完成它。當然你也可以使用LINQ。但是我選擇了.NET 2.0方法,因爲我不確定你使用的是哪個版本的.NET。
XmlDocument doc = new XmlDocument();
// Create the Where Node
XmlNode whereNode = doc.CreateNode(XmlNodeType.Element, "Where", string.Empty);
XmlNode eqNode = doc.CreateNode(XmlNodeType.Element, "Eq", string.Empty);
XmlNode fieldNode = doc.CreateNode(XmlNodeType.Element, "Field", string.Empty);
XmlAttribute newAttribute = doc.CreateAttribute("FieldName");
newAttribute.InnerText = "Name";
fieldNode.Attributes.Append(newAttribute);
XmlNode valueNode = doc.CreateNode(XmlNodeType.Element, "Value", string.Empty);
XmlAttribute valueAtt = doc.CreateAttribute("Type");
valueAtt.InnerText = "Text";
valueNode.Attributes.Append(valueAtt);
// Can set the text of the Node to anything.
valueNode.InnerText = "Value Text";
// Or you can use
//valueNode.InnerXml = "<aValid>SomeStuff</aValid>";
// Create the document
fieldNode.AppendChild(valueNode);
eqNode.AppendChild(fieldNode);
whereNode.AppendChild(eqNode);
doc.AppendChild(whereNode);
// Or you can use XQuery to Find the node and then change it
// Find the Where Node
XmlNode foundWhereNode = doc.SelectSingleNode("Where/Eq/Field/Value");
if (foundWhereNode != null)
{
// Now you can set the Value
foundWhereNode.InnerText = "Some Value Text";
}
我與.NET 2.0 – axk 2008-09-16 10:05:10