日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

如何在ASP.net(C#)下操作XML文件_.Net教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦: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","hl5o.cn");//設(shè)置該節(jié)點genre屬性
websiteElement.SetAttribute("ISBN","2-3631-4");//設(shè)置該節(jié)點ISBN屬性
XmlElement titleElement=xmldoc.CreateElement("title");
titleElement.InnerText="
模板無憂";//設(shè)置文本節(jié)點
websiteElement.AppendChild(titleElement);//添加到<Website>節(jié)點中
XmlElement authorElement=xmldoc.CreateElement("author");
authorElement.InnerText="
作者";
websiteElement.AppendChild(authorElement);
XmlElement urlElement=xmldoc.CreateElement("url");
urlElement.InnerText="http://hl5o.cn";
websiteElement.AppendChild(urlElement);
rootElement.AppendChild(websiteElement);
//添加到<Websites>節(jié)點中
}
//保存創(chuàng)建好的XML文檔
xmldoc.Save ( Server.MapPath("database.xml") ) ;


結(jié)果:

<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://hl5o.cn</url>
</Website>
<Website genre="hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://hl5o.cn</url>
</Website>
</Websites>

//database.xml文件內(nèi)容如下

2,添加結(jié)點:

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.#");//設(shè)置該節(jié)點genre屬性
websiteElement.SetAttribute("ISBN","1-1111-1");//設(shè)置該節(jié)點ISBN屬性
XmlElement titleElement=xmlDoc.CreateElement("title");
titleElement.InnerText="
站長統(tǒng)計";//設(shè)置文本節(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") );

結(jié)果:

<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://hl5o.cn</url>
</Website>
<Website genre="hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://hl5o.cn</url>
</Website>
<Website genre="www.#" ISBN="1-1111-1">
<title>站長統(tǒng)計</title>
<author>站長</author>
<url>http://www.#</url>
</Website>
</Websites>

3,修改結(jié)點的值:

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é)點類型轉(zhuǎn)換為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;
//轉(zhuǎn)換類型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="
作者";//則修改
}
}
}
}
xmlDoc.Save( Server.MapPath("database.xml") );
//保存。

結(jié)果:

<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://hl5o.cn</url>
</Website>
<Website genre="hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://hl5o.cn</url>
</Website>
<Website genre="updatewww.#" ISBN="1-1111-1">
<title>站長統(tǒng)計</title>
<author>作者</author>
<url>http://www.#</url>
</Website>
</Websites>

4,修改結(jié)點

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

結(jié)果:

<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="hl5o.cn" ISBN="2-3631-4" test="99999">
<title>模板無憂</title>
<author>作者</author>
<url>http://hl5o.cn</url>
<fffff>1</fffff>
</Website>
<Website genre="hl5o.cn" ISBN="2-3631-4" test="99999">
<title>模板無憂</title>
<author>作者</author>
<url>http://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,刪除結(jié)點中的某一個屬性:

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;
//轉(zhuǎn)換類型
if(xe2.Name=="fffff")//如果找到
{
xe.RemoveChild(xe2);
//則刪除
}
}
}
xmlDoc.Save( Server.MapPath("database.xml") );


結(jié)果:

<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website ISBN="2-3631-4" test="99999">
<title>模板無憂</title>
<author>作者</author>
<url>http://hl5o.cn</url>
</Website>
<Website ISBN="2-3631-4" test="99999">
<title>模板無憂</title>
<author>作者</author>
<url>http://hl5o.cn</url>
</Website>
<Website ISBN="1-1111-1" test="99999">
<title>站長統(tǒng)計</title>
<author>作者</author>
<url>http://www.#</url>
</Website>
</Websites>

6,刪除結(jié)點:

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

結(jié)果:刪除了符合條件的所有結(jié)點,原來的內(nèi)容:

 

<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://hl5o.cn</url>
</Website>
<Website genre="hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://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="hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://hl5o.cn</url>
</Website>
<Website genre="hl5o.cn" ISBN="2-3631-4">
<title>模板無憂</title>
<author>作者</author>
<url>http://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=&

來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
相關(guān).Net教程