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();