xml
创建XML
//创建XmlDocument对象 XmlDocument xmlDoc = new XmlDocument(); //XML的声明 <?xml version="1.0" encoding="gb2312"?> XmlDeclaration desc = XmlDoc.CreateXmlDeclaration("1.0","utf-8",""); //追加xml的位置 XmlDoc.AppendChild(desc); //添加根节点 XmlElement xmlelement = XmlDoc.CreateElement("","Gen",""); XmlDoc.AppendChild(xmlelement); XmlNode node = XmlDoc.SelectSingleNode("Gen"); XmlElement son = XmlDoc.CreateElement("son"); son.SetAttribute("name","xue"); son.SetAttribute("age","25"); XmlElement element = XmlDoc.CreateElement("title"); element.InnerText = "c#"; son.AppendChild(element);//添加子节点到第一个子节点 node.AppendChild(son);//添加子节点到根节点 XmlDoc.Save("D:/data.xml");
修改xml文件里面已经有了描述和根节点
//创建xml的根节点 XmlDoc.Load(path); XmlNode node = XmlDoc.SelectSingleNode("config/"+key);//查找文件里面的要修改的节点 if (node == null) { XmlElement t = XmlDoc.CreateElement(key); t.InnerText = value; XmlDoc.SelectSingleNode("config").AppendChild(t); } else { node.InnerText = value; } XmlDoc.Save(path);
111111