读:
XmlDocument xmlDoc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;
XmlReader reader = null;
reader = XmlReader.Create(fileName, settings);
xmlDoc.Load(reader);
XmlNode root = xmlDoc.SelectSingleNode("Root");
XmlAttribute xmlAttr = root.Attributes["clazz"];
String type = xmlAttr == null ? null : xmlAttr.Value;
foreach(XmlNode subNode in root){
subNode.Value;
}
写:
XmlDeclaration Declaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null)
xmlDoc.InsertBefore(Declaration, xmlDoc.DocumentElement)
//创建Root节点
XmlNode rootNode = xmlDoc.CreateElement("Root")
xmlDoc.AppendChild(rootNode)
//Root的子节点branch
XmlNode branch=xmlDoc.CreateElement("branch")
rootNode.AppendChild(branch)
//给branch添加属性type="branch"
XmlAttribute attr=xmlDoc.CreateAttribute("type")
attr.Value="branch"
branch.Attributes.Append(attr)
//保存到文件
xmlDoc.save(fileName)