在C#中使用XmlDocument类可以很方便地操作XML。以下是一些常见的用法:
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("path/to/xml/file.xml"); xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); XmlElement rootElement = xmlDoc.CreateElement("Root"); xmlDoc.AppendChild(rootElement); XmlElement rootElement = xmlDoc.DocumentElement; XmlNodeList nodeList = xmlDoc.SelectNodes("//NodeName"); foreach (XmlNode node in nodeList) { // 处理节点 } string attributeValue = node.Attributes["AttributeName"].Value; XmlElement newNode = xmlDoc.CreateElement("NewNode"); parentNode.AppendChild(newNode); node.Attributes["AttributeName"].Value = "NewValue"; parentNode.RemoveChild(node); xmlDoc.Save("path/to/save/xml/file.xml"); 以上是一些常见的XmlDocument的用法,你可以根据具体的需求进行操作。