已知有一个XML文件(bookshop.xml)如下:
<?xml version="1.0" encoding="gb2312" ?> <bookshop> <book genre="fantasy" ISBN="2-2312-2"> <title>Oberon Legacy</title> <author>Eva</author> <price>56.5</price> </book> </bookshop>
下面看看是怎样读写xml文件的:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace ConsoleApplication1 { class Program { XmlDocument xmlDoc; ///<summary> /// 插入节点 ///</summary> publicvoid InsertNode() { xmlDoc =new XmlDocument(); xmlDoc.Load("bookshop.xml"); //加载xml文件 /*从指定的字符创加载xml文件 例如: xmlDoc.LoadXml("(<Book bookID='B001'><BookName>jeff</BookName><price>45.6</price></Book>)"); */ XmlNode root = xmlDoc.SelectSingleNode("bookshop");//查找﹤bookstore﹥ XmlElement xe1 = xmlDoc.CreateElement("book");//创建一个﹤book﹥节点 xe1.SetAttribute("genre", "Sky_Kwolf");//设置该节点genre属性 xe1.SetAttribute("ISBN", "2-3631-4");//设置该节点ISBN属性 XmlElement xesub1 = xmlDoc.CreateElement("title"); xesub1.InnerText ="CSS禅意花园";//设置节点的文本值 xe1.AppendChild(xesub1);//添加到﹤book﹥节点中 XmlElement xesub2 = xmlDoc.CreateElement("author"); xesub2.InnerText ="Jeff"; xe1.AppendChild(xesub2); XmlElement xesub3 = xmlDoc.CreateElement("price"); xesub3.InnerText ="58.3"; xe1.AppendChild(xesub3); root.AppendChild(xe1);//添加到﹤bookshop﹥节点中 xmlDoc.Save("bookshop.xml"); //保存其更改 } ///<summary> /// 修改节点 ///</summary> publicvoid UpdateNode() { xmlDoc =new XmlDocument(); xmlDoc.Load("bookshop.xml"); //加载xml文件 //获取bookshop节点的所有子节点 XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookshop").ChildNodes; //遍历所有子节点 foreach (XmlNode xn in nodeList) { XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 if (xe.GetAttribute("genre") =="Sky_Kwolf")//如果genre属性值为“Sky_Kwolf” { xe.SetAttribute("genre", "update Sky_Kwolf"); //则修改该属性为“update Sky_Kwolf” XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点 foreach (XmlNode xn1 in nls)//遍历 { XmlElement xe2 = (XmlElement)xn1; //转换类型 if (xe2.Name =="author")//如果找到 { xe2.InnerText ="jason";//则修改 break;//找到退出 } } break; } } xmlDoc.Save("bookshop.xml");//保存。 } //显示xml数据 publicvoid ShowXml() { xmlDoc =new XmlDocument(); xmlDoc.Load("bookshop.xml"); //加载xml文件 XmlNode xn = xmlDoc.SelectSingleNode("bookshop"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl) { XmlElement xe = (XmlElement)xnf; Console.WriteLine(xe.GetAttribute("genre"));//显示属性值 Console.WriteLine(xe.GetAttribute("ISBN")); XmlNodeList xnf1 = xe.ChildNodes; foreach (XmlNode xn2 in xnf1) { Console.WriteLine(xn2.InnerText);//显示子节点点文本 } } } ///<summary> /// 删除节点 ///</summary> publicvoid DeleteNode() { xmlDoc =new XmlDocument(); xmlDoc.Load("bookshop.xml"); //加载xml文件 XmlNodeList xnl = xmlDoc.SelectSingleNode("bookshop").ChildNodes; foreach (XmlNode xn in xnl) { XmlElement xe = (XmlElement)xn; if (xe.GetAttribute("genre") =="fantasy") { xe.RemoveAttribute("genre");//删除genre属性 } elseif (xe.GetAttribute("genre") =="update Sky_Kwolf") { xe.RemoveAll();//删除该节点的全部内容 } } xmlDoc.Save("bookshop.xml"); } staticvoid Main(string[] args) { Program p =new Program(); p.ShowXml(); Console.ReadLine(); } } }
XmlDocument XmlDoc = null; XmlDoc = new XmlDocument(); XmlDeclaration dec = XmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); XmlDoc.AppendChild(dec); XmlElement root = XmlDoc.CreateElement("message"); XmlDoc.AppendChild(root); XmlElement eName; XmlNode M_Root = XmlDoc.SelectSingleNode("message"); XmlNode M_Root1; XmlElement head = XmlDoc.CreateElement("header"); head.SetAttribute("checksum", "2e11193e4d27dc9b0600de7b2586b1e4"); root.AppendChild(head); eName = XmlDoc.CreateElement("bizid");//创建节点 eName.InnerXml = "100001"; head.AppendChild(eName); eName = XmlDoc.CreateElement("body");//创建节点 eName.InnerXml = "1"; head.AppendChild(eName); XmlElement cont = XmlDoc.CreateElement("content"); root.AppendChild(cont); M_Root1 = XmlDoc.SelectSingleNode("message").LastChild; XmlElement table = XmlDoc.CreateElement("Table"); table.SetAttribute("count", "1"); cont.AppendChild(table); XmlElement row = XmlDoc.CreateElement("row"); row.SetAttribute("id", "1"); table.AppendChild(row); eName = XmlDoc.CreateElement("column");//创建节点 eName.SetAttribute("type", "String"); eName.SetAttribute("id", "BT"); eName.SetAttribute("desc", "题名"); eName.InnerXml = "<![CDATA[" + ds.Tables[0].Rows[i]["BT"].ToString() + "]]>"; row.AppendChild(eName); string file = FilePath + ds.Tables[0].Rows[i]["ID"].ToString() + "REST.XML"; XmlDoc.Save(file); //保存XML return XmlDoc.InnerXml;//显示XML