C# 操作xml(新建,添加,修改,删除)
来源:互联网 发布:数据备份技术如何分类 编辑:程序博客网 时间:2024/06/05 11:45
<?xml version="1.0"?><root> <book isbn="111" id="1111"> <title>C#</title> </book> <book isbn="222" id="2222"> <title>F#</title> </book> <book isbn="333" id="3333"> <title>B#</title> </book></root>
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.IO;namespace WindowsFormsApplication1{ public class OutXml:IDisposable { public static readonly object obj = new object(); public XmlDocument xmldoc=null; public OutXml() { if (xmldoc == null) { lock (obj) { if (xmldoc == null) { xmldoc = new XmlDocument(); if (File.Exists(@"d:\book.xml")) { File.Delete(@"d:\book.xml"); } //File.Create(@"d:\book.xml"); XmlDeclaration xd = xmldoc.CreateXmlDeclaration("1.0", null, null); xmldoc.AppendChild(xd); XmlElement newroot = xmldoc.CreateElement("root"); xmldoc.AppendChild(newroot); xmldoc.Save(@"d:\book.xml"); xmldoc.Load(@"d:\book.xml"); } } } } //生成XML文件 public void setXML() { XmlElement xe = xmldoc.CreateElement("book"); xe.SetAttribute("isbn", "111"); XmlAttribute xa = xmldoc.CreateAttribute("id"); xa.Value = "1111"; xe.SetAttributeNode(xa); XmlElement xe2 = xmldoc.CreateElement("title"); xe2.InnerText = "C#"; xe.AppendChild(xe2); xmldoc.DocumentElement.AppendChild(xe); xe = xmldoc.CreateElement("book"); xe.SetAttribute("isbn", "222"); xa = xmldoc.CreateAttribute("id"); xa.Value = "2222"; xe.SetAttributeNode(xa); xe2 = xmldoc.CreateElement("title"); xe2.InnerText = "F#"; xe.AppendChild(xe2); xmldoc.DocumentElement.AppendChild(xe); xmldoc.DocumentElement.AppendChild(xe.Clone());
xmldoc.DocumentElement.AppendChild(xe.CloneNode(true));//拷贝xe的所有内容(当前和内部的xml)
//xmldoc.DocumentElement.AppendChild(xe.CloneNode(false));//不拷贝xe的内部内容(只取得当前xml的相关属性,不取得内部xml xe = xmldoc.CreateElement("book"); xe.SetAttribute("isbn", "333"); xa = xmldoc.CreateAttribute("id"); xa.Value = "3333"; xe.SetAttributeNode(xa); xe2 = xmldoc.CreateElement("title"); xe2.InnerText = "B#"; xe.AppendChild(xe2); xmldoc.DocumentElement.AppendChild(xe); //XmlDeclaration XmlTextWriter tr = new XmlTextWriter(@"d:\book.xml", null); tr.Formatting = Formatting.Indented; xmldoc.WriteContentTo(tr); tr.Close(); } // 读取xml文件 public string readXML(string path) { string readcontent = string.Empty; XmlNodeList xnl = xmldoc.SelectNodes(path); foreach (XmlNode item in xnl) { XmlElement xetemp = (XmlElement)item; readcontent += xetemp.FirstChild.InnerText +"\r\n"; } return readcontent; } // 读取xml文件 public string readAllXML(string path) { string readcontent = string.Empty; XmlNodeList xnl = xmldoc.SelectNodes(path); foreach (XmlNode item in xnl) { XmlElement xetemp = (XmlElement)item; readcontent += xetemp.OuterXml + "\r\n"; //readcontent += item["title"].OuterXml + "\r\n"; } return readcontent; } //添加xml public void AddXML(string path) { XmlNodeList xnl = xmldoc.SelectNodes(path); foreach (XmlNode item in xnl) { XmlElement xetemp = (XmlElement)item; if (xetemp.GetAttribute("Anthor") == "") { xetemp.SetAttribute("Anthor", "randy"); } if (xetemp.GetElementsByTagName("Price").Count <1) { XmlElement price= xmldoc.CreateElement("price"); price.InnerText="100"; xetemp.AppendChild(price); } } xmldoc.Save(@"d:\book.xml"); } //修改xml public void UpdateXML(string path) { XmlNodeList xnl = xmldoc.SelectNodes(path); foreach (XmlNode item in xnl) { XmlElement xe = (XmlElement)item; if (xe.GetAttribute("Anthor") != string.Empty) { xe.SetAttribute("Anthor", "Pulisher"); } if (xe.GetElementsByTagName("price").Count > 0) { xe =(XmlElement)xe.SelectSingleNode("price"); xe.InnerText = "90"; } } xmldoc.Save(@"d:\book.xml"); } public void DelXML(string path) { XmlNodeList xnl = xmldoc.SelectNodes(path); foreach (XmlNode item in xnl) { item.ParentNode.RemoveChild(item); } xmldoc.Save(@"d:\book.xml"); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!m_disposed) { if (disposing) { this.Dispose(); } m_disposed = true; } } ~OutXml() { Dispose(false); } private bool m_disposed; }}