4,修改結點
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("database.xml") );
XmlNodeList nodeList=xmlDoc.SelectSingleNode("Websites").ChildNodes;//獲取Websites節點的所有子節點
foreach(XmlNode xn in nodeList)
{
XmlElement xe=(XmlElement)xn;
xe.SetAttribute("test","99999");
XmlElement xesub=xmlDoc.CreateElement("fffff");
xesub.InnerText="1";
xe.AppendChild(xesub);
}
xmlDoc.Save( Server.MapPath("database.xml") );
結果:
<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="www.chinaz.com" ISBN="2-3631-4" test="99999">
<title>中國站長站</title>
<author>作者</author>
<url>http://www.chinaz.com</url>
<fffff>1</fffff>
</Website>
<Website genre="www.chinaz.com" ISBN="2-3631-4" test="99999">
<title>中國站長站</title>
<author>作者</author>
<url>http://www.chinaz.com</url>
<fffff>1</fffff>
</Website>
<Website genre="updatewww.cnzz.com" ISBN="1-1111-1" test="99999">
<title>站長統計</title>
<author>作者</author>
<url>http://www.cnzz.com</url>
<fffff>1</fffff>
</Website>
</Websites>
5,刪除結點中的某一個屬性:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("database.xml") );
XmlNodeList xnl=xmlDoc.SelectSingleNode("Websites").ChildNodes;
foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
xe.RemoveAttribute("genre");//刪除genre屬性
XmlNodeList nls=xe.ChildNodes;//繼續獲取xe子節點的所有子節點
foreach(XmlNode xn1 in nls)//遍歷
{
XmlElement xe2=(XmlElement)xn1;//轉換類型
if(xe2.Name=="fffff")//如果找到
{
xe.RemoveChild(xe2);//則刪除
}
}
}
xmlDoc.Save( Server.MapPath("database.xml") );
結果:
<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website ISBN="2-3631-4" test="99999">
<title>中國站長站</title>
<author>作者</author>
<url>http://www.chinaz.com</url>
</Website>
<Website ISBN="2-3631-4" test="99999">
<title>中國站長站</title>
<author>作者</author>
<url>http://www.chinaz.com</url>
</Website>
<Website ISBN="1-1111-1" test="99999">
<title>站長統計</title>
<author>作者</author>
<url>http://www.cnzz.com</url>
</Website>
</Websites>
6,刪除結點:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("database.xml") );
XmlNode rootElement=xmlDoc.SelectSingleNode("Websites");
XmlNodeList xnl=xmlDoc.SelectSingleNode("Websites").ChildNodes;
for(int i=0;i<xnl.Count;i++)
{
XmlElement xe=(XmlElement)xnl.Item(i);
if(xe.GetAttribute("genre")=="www.cnzz.com")
{
rootElement.RemoveChild(xe);
if(i<xnl.Count)i=i-1;
}
}
xmlDoc.Save( Server.MapPath("database.xml") );
結果:刪除了符合條件的所有結點,原來的內容:
<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="www.chinaz.com" ISBN="2-3631-4">
<title>中國站長站</title>
<author>作者</author>
<url>http://www.chinaz.com</url>
</Website>
<Website genre="www.chinaz.com" ISBN="2-3631-4">
<title>中國站長站</title>
<author>作者</author>
<url>http://www.chinaz.com</url>
</Website>
<Website genre="www.cnzz.com" ISBN="1-1111-1">
<title>站長統計</title>
<author>站長</author>
<url>http://www.cnzz.com</url>
</Website>
<Website genre="www.cnzz.com" ISBN="1-1111-1">
<title>站長統計</title>
<author>站長</author>
<url>http://www.cnzz.com</url>
</Website>
</Websites>
刪除後的內容:
<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="www.chinaz.com" ISBN="2-3631-4">
<title>中國站長站</title>
<author>作者</author>
<url>http://www.chinaz.com</url>
</Website>
<Website genre="www.chinaz.com" ISBN="2-3631-4">
<title>中國站長站</title>
<author>作者</author>
<url>http://www.chinaz.com</url>
</Website>
</Websites>
7,按照文本文件讀取xml
System.IO.StreamReader myFile =new
System.IO.StreamReader(Server.MapPath("database.xml"),System.Text.Encoding.Default);
//注意System.Text.Encoding.Default
string myString = myFile.ReadToEnd();//myString是讀出的字符串
myFile.Close();