如何在ASP.net(C#)下操作XML文件_.Net教程
推薦:ASP.NET生成靜態(tài)HTML頁面并分別按年月目錄存放一說到新聞系統(tǒng)的話,一定會談到靜態(tài)頁面生成的,因為靜態(tài)頁面不但是讀取速度快,而且又安全; 靜態(tài)頁面的生成不管是小到現(xiàn)在的企業(yè)網(wǎng)站大至網(wǎng)易,QQ等門戶都用到了; 那么我們?nèi)绾蝸砩?/p>
本文將重點介紹如何在ASP.net(C#)下操作XML文件。
1,創(chuàng)建xml文件:
代碼:
XmlDocument xmldoc = new XmlDocument ( ) ;
//加入XML的聲明段落,<?xml version="1.0" encoding="gb2312"?>
XmlDeclaration xmldecl;
xmldecl = xmldoc.CreateXmlDeclaration("1.0","gb2312",null);
xmldoc.AppendChild ( xmldecl);
//加入一個根元素
XmlElement xmlelem = xmldoc.CreateElement( "" , "Websites" , "" ) ;
xmldoc.AppendChild ( xmlelem ) ;
//加入另外一個元素
for(int i=1;i<3;i )
{
XmlNode rootElement=xmldoc.SelectSingleNode("Websites");//查找<Websites>
XmlElement websiteElement=xmldoc.CreateElement("Website");//創(chuàng)建一個<Website>節(jié)點
websiteElement.SetAttribute("genre","www.hl5o.cn");//設置該節(jié)點genre屬性
websiteElement.SetAttribute("ISBN","2-3631-4");//設置該節(jié)點ISBN屬性
XmlElement titleElement=xmldoc.CreateElement("title");
titleElement.InnerText="模板無憂";//設置文本節(jié)點
websiteElement.AppendChild(titleElement);//添加到<Website>節(jié)點中
XmlElement authorElement=xmldoc.CreateElement("author");
authorElement.InnerText="作者";
websiteElement.AppendChild(authorElement);
XmlElement urlElement=xmldoc.CreateElement("url");
urlElement.InnerText="http://www.hl5o.cn";
websiteElement.AppendChild(urlElement);
rootElement.AppendChild(websiteElement);//添加到<Websites>節(jié)點中
}
//保存創(chuàng)建好的XML文檔
xmldoc.Save ( Server.MapPath("database.xml") ) ;
結果:
<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="www.hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
</Website>
<Website genre="www.hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
</Website>
</Websites>
//database.xml文件內(nèi)容如下
2,添加結點:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(Server.MapPath("database.xml"));
XmlNode rootElement=xmlDoc.SelectSingleNode("Websites");//查找<Websites>
XmlElement websiteElement=xmlDoc.CreateElement("Website");//創(chuàng)建一個<Website>節(jié)點
websiteElement.SetAttribute("genre","www.#");//設置該節(jié)點genre屬性
websiteElement.SetAttribute("ISBN","1-1111-1");//設置該節(jié)點ISBN屬性
XmlElement titleElement=xmlDoc.CreateElement("title");
titleElement.InnerText="站長統(tǒng)計";//設置文本節(jié)點
websiteElement.AppendChild(titleElement);//添加到<Website>節(jié)點中
XmlElement authorElement=xmlDoc.CreateElement("author");
authorElement.InnerText="站長";
websiteElement.AppendChild(authorElement);
XmlElement urlElement=xmlDoc.CreateElement("url");
urlElement.InnerText="http://www.#";
websiteElement.AppendChild(urlElement);
rootElement.AppendChild(websiteElement);//添加到<Websites>節(jié)點中
xmlDoc.Save ( Server.MapPath("database.xml") );
結果:
<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="www.hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
</Website>
<Website genre="www.hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
</Website>
<Website genre="www.#" ISBN="1-1111-1">
<title>站長統(tǒng)計</title>
<author>站長</author>
<url>http://www.#</url>
</Website>
</Websites>
3,修改結點的值:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("database.xml") );
XmlNodeList nodeList=xmlDoc.SelectSingleNode("Websites").ChildNodes;//獲取Websites節(jié)點的所有子節(jié)點
foreach(XmlNode xn in nodeList)//遍歷所有子節(jié)點
{
XmlElement xe=(XmlElement)xn;//將子節(jié)點類型轉換為XmlElement類型
if(xe.GetAttribute("genre")=="www.#")//如果genre屬性值為“www.#”
{
xe.SetAttribute("genre","updatewww.#");//則修改該屬性為“updatewww.#”
XmlNodeList nls=xe.ChildNodes;//繼續(xù)獲取xe子節(jié)點的所有子節(jié)點
foreach(XmlNode xn1 in nls)//遍歷
{
XmlElement xe2=(XmlElement)xn1;//轉換類型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="作者";//則修改
}
}
}
}
xmlDoc.Save( Server.MapPath("database.xml") );//保存。
結果:
<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="www.hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
</Website>
<Website genre="www.hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
</Website>
<Website genre="updatewww.#" ISBN="1-1111-1">
<title>站長統(tǒng)計</title>
<author>作者</author>
<url>http://www.#</url>
</Website>
</Websites>
4,修改結點
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("database.xml") );
XmlNodeList nodeList=xmlDoc.SelectSingleNode("Websites").ChildNodes;//獲取Websites節(jié)點的所有子節(jié)點
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.hl5o.cn" ISBN="2-3631-4" test="99999">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
<fffff>1</fffff>
</Website>
<Website genre="www.hl5o.cn" ISBN="2-3631-4" test="99999">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
<fffff>1</fffff>
</Website>
<Website genre="updatewww.#" ISBN="1-1111-1" test="99999">
<title>站長統(tǒng)計</title>
<author>作者</author>
<url>http://www.#</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;//繼續(xù)獲取xe子節(jié)點的所有子節(jié)點
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.hl5o.cn</url>
</Website>
<Website ISBN="2-3631-4" test="99999">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
</Website>
<Website ISBN="1-1111-1" test="99999">
<title>站長統(tǒng)計</title>
<author>作者</author>
<url>http://www.#</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.#")
{
rootElement.RemoveChild(xe);
if(i<xnl.Count)i=i-1;
}
}
xmlDoc.Save( Server.MapPath("database.xml") );
結果:刪除了符合條件的所有結點,原來的內(nèi)容:
<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="www.hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
</Website>
<Website genre="www.hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
</Website>
<Website genre="www.#" ISBN="1-1111-1">
<title>站長統(tǒng)計</title>
<author>站長</author>
<url>http://www.#</url>
</Website>
<Website genre="www.#" ISBN="1-1111-1">
<title>站長統(tǒng)計</title>
<author>站長</author>
<url>http://www.#</url>
</Website>
</Websites>
刪除后的內(nèi)容:
<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="www.hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</url>
</Website>
<Website genre="www.hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://www.hl5o.cn</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();
分享:ASP.Net中利用CSS實現(xiàn)多界面兩法通過使頁面動態(tài)加載不同CSS實現(xiàn)多界面 (類似于這個blog) 方法一: 以下為引用的內(nèi)容: <%@page language="C#"%> <%@import namespace=&
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實例(可帶附件)
- js實現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實現(xiàn)
- Asp.Net 無刷新文件上傳并顯示進度條的實現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(連接字符串的配置及獲取)
- asp.net頁面?zhèn)髦禍y試實例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲過程實現(xiàn)分頁示例代碼
- 相關鏈接:
- 教程說明:
.Net教程-如何在ASP.net(C#)下操作XML文件
。