wbcmlb 2006-5-9 18:18
XML的简单读取与写入
<?xml version="1.0" encoding="gb2312"?><BR><bookstore><BR><book genre="fantasy" ISBN="2-3631-4"><BR><t[wiki]IT[/wiki]le>Oberon's Legacy</title><BR><author>Corets, Eva</author><BR><[WIKI]PR[/wiki]ice>5.95</price><BR></book><BR></bookstore><BR><BR>1、往<bookstore>节点中插入一个<book>节点:<BR>XmlDocument xmlDoc=new XmlDocument();<BR>xmlDoc.Load("bookstore.xml");<BR>XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore><BR>XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点<BR>xe1.SetAttribute("genre","李赞红");//设置该节点genre属性<BR>xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性<BR><BR>XmlElement xesub1=xmlDoc.CreateElement("title");<BR>xesub1.InnerText="CS从入门到精通";//设置文本节点<BR>xe1.AppenDChild(xesub1);//添加到<book>节点中<BR>XmlElement xesub2=xmlDoc.CreateElement("author");<BR>xesub2.InnerText="候捷";<BR>xe1.AppendChild(xesub2);<BR>XmlElement xesub3=xmlDoc.CreateElement("price");<BR>xesub3.InnerText="58.3";<BR>xe1.AppendChild(xesub3);<BR><BR>root.AppendChild(xe1);//添加到<bookstore>节点中<BR>xmlDoc.Save("bookstore.xml");<BR>//===============================================<BR>结果为:<BR><?xml version="1.0" encoding="gb2312"?><BR><bookstore><BR><book genre="fantasy" ISBN="2-3631-4"><BR><title>Oberon's Legacy</title><BR><author>Corets, Eva</author><BR><price>5.95</price><BR></book><BR><book genre="李赞红" ISBN="2-3631-4"><BR><title>CS从入门到精通</title><BR><author>候捷</author><BR><price>58.3</price><BR></book><BR></bookstore><BR><BR>2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。<BR>XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点<BR>foreach(XmlNode xn in nodeList)//遍历所有子节点<BR>{<BR>XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型<BR>if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”<BR>{<BR>xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”<BR><BR>XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点<BR>foreach(XmlNode xn1 in nls)//遍历<BR>{<BR>XmlElement xe2=(XmlElement)xn1;//转换类型<BR>if(xe2.Name=="author")//如果找到<BR>{<BR>xe2.InnerText="亚胜";//则修改<BR>break;//找到退出来就可以了<BR>}<BR>}<BR>break;<BR>}<BR>}<BR><BR>xmlDoc.Save("bookstore.xml");//保存。<BR>//==================================================<BR>最后结果为:<BR><?xml version="1.0" encoding="gb2312"?><BR><bookstore><BR><book genre="fantasy" ISBN="2-3631-4"><BR><title>Oberon's Legacy</title><BR><author>Corets, Eva</author><BR><price>5.95</price><BR></book><BR><book genre="update李赞红" ISBN="2-3631-4"><BR><title>CS从入门到精通</title><BR><author>亚胜</author><BR><price>58.3</price><BR></book><BR></bookstore><BR><BR>3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。<BR>XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;<BR><BR>foreach(XmlNode xn in xnl)<BR>{<BR>XmlElement xe=(XmlElement)xn;<BR>if(xe.GetAttribute("genre")=="fantasy")<BR>{<BR>xe.RemoveAttribute("genre");//删除genre属性<BR>}<BR>else if(xe.GetAttribute("genre")=="update李赞红")<BR>{<BR>xe.RemoveAll();//删除该节点的全部内容<BR>}<BR>}<BR>xmlDoc.Save("bookstore.xml");<BR>//===========================================<BR>最后结果为:<BR><?xml version="1.0" encoding="gb2312"?><BR><bookstore><BR><book ISBN="2-3631-4"><BR><title>Oberon's Legacy</title><BR><author>Corets, Eva</author><BR><price>5.95</price><BR></book><BR><book><BR></book><BR></bookstore><BR><BR>4、显示所有数据。<BR>XmlNode xn=xmlDoc.SelectSingleNode("bookstore");<BR><BR>XmlNodeList xnl=xn.ChildNodes;<BR><BR>foreach(XmlNode xnf in xnl)<BR>{<BR>XmlElement xe=(XmlElement)xnf;<BR>Console.WriteLine(xe.GetAttribute("genre"));//显示属性值<BR>Console.WriteLine(xe.GetAttribute("ISBN"));<BR><BR>XmlNodeList xnf1=xe.ChildNodes;<BR>foreach(XmlNode xn2 in xnf1)<BR>{<BR>Console.WriteLine(xn2.InnerText);//显示子节点点文本<BR>}<BR>