您可以使用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