国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 12:50:04
字體:
來源:轉載
供稿:網友

推薦:ASP.NET生成靜態HTML頁面并分別按年月目錄存放
一說到新聞系統的話,一定會談到靜態頁面生成的,因為靜態頁面不但是讀取速度快,而且又安全;靜態頁面的生成不管是小到現在的企業網站大至網易,QQ等門戶都用到了;那么我們如何來生成

本文將重點介紹如何在ASP.net(C#)下操作XML文件。

1,創建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");//創建一個<Website>節點
websiteElement.SetAttribute("genre","www.CuoXIn.com");//設置該節點genre屬性
websiteElement.SetAttribute("ISBN","2-3631-4");//設置該節點ISBN屬性
XmlElement titleElement=xmldoc.CreateElement("title");
titleElement.InnerText="
武林網";//設置文本節點
websiteElement.AppendChild(titleElement);//添加到<Website>節點中
XmlElement authorElement=xmldoc.CreateElement("author");
authorElement.InnerText="
作者";
websiteElement.AppendChild(authorElement);
XmlElement urlElement=xmldoc.CreateElement("url");
urlElement.InnerText="http://www.CuoXIn.com";
websiteElement.AppendChild(urlElement);
rootElement.AppendChild(websiteElement);
//添加到<Websites>節點中
}
//保存創建好的XML文檔
xmldoc.Save ( Server.MapPath("database.xml") ) ;


結果:

<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="www.CuoXIn.com" ISBN="2-3631-4">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.com</url>
</Website>
<Website genre="www.CuoXIn.com" ISBN="2-3631-4">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.com</url>
</Website>
</Websites>

//database.xml文件內容如下

2,添加結點:

XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(Server.MapPath("database.xml"));
XmlNode rootElement=xmlDoc.SelectSingleNode("Websites");
//查找<Websites>
XmlElement websiteElement=xmlDoc.CreateElement("Website");//創建一個<Website>節點
websiteElement.SetAttribute("genre","www.cnzz.com");//設置該節點genre屬性
websiteElement.SetAttribute("ISBN","1-1111-1");//設置該節點ISBN屬性
XmlElement titleElement=xmlDoc.CreateElement("title");
titleElement.InnerText="
站長統計";//設置文本節點
websiteElement.AppendChild(titleElement);//添加到<Website>節點中
XmlElement authorElement=xmlDoc.CreateElement("author");
authorElement.InnerText="
站長";
websiteElement.AppendChild(authorElement);
XmlElement urlElement=xmlDoc.CreateElement("url");
urlElement.InnerText="http://www.cnzz.com";
websiteElement.AppendChild(urlElement);
rootElement.AppendChild(websiteElement);
//添加到<Websites>節點中
xmlDoc.Save ( Server.MapPath("database.xml") );

結果:

<?xml version="1.0" encoding="gb2312"?>
<Websites>
<Website genre="www.CuoXIn.com" ISBN="2-3631-4">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.com</url>
</Website>
<Website genre="www.CuoXIn.com" ISBN="2-3631-4">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.com</url>
</Website>
<Website genre="www.cnzz.com" ISBN="1-1111-1">
<title>站長統計</title>
<author>站長</author>
<url>http://www.cnzz.com</url>
</Website>
</Websites>

3,修改結點的值:

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;
//將子節點類型轉換為XmlElement類型
if(xe.GetAttribute("genre")=="www.cnzz.com")//如果genre屬性值為“www.cnzz.com”
{
xe.SetAttribute("genre","updatewww.cnzz.com");
//則修改該屬性為“updatewww.cnzz.com”
XmlNodeList nls=xe.ChildNodes;//繼續獲取xe子節點的所有子節點
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.CuoXIn.com" ISBN="2-3631-4">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.com</url>
</Website>
<Website genre="www.CuoXIn.com" ISBN="2-3631-4">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.com</url>
</Website>
<Website genre="updatewww.cnzz.com" ISBN="1-1111-1">
<title>站長統計</title>
<author>作者</author>
<url>http://www.cnzz.com</url>
</Website>
</Websites>

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.CuoXIn.com" ISBN="2-3631-4" test="99999">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.com</url>
<fffff>1</fffff>
</Website>
<Website genre="www.CuoXIn.com" ISBN="2-3631-4" test="99999">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.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.CuoXIn.com</url>
</Website>
<Website ISBN="2-3631-4" test="99999">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.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.CuoXIn.com" ISBN="2-3631-4">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.com</url>
</Website>
<Website genre="www.CuoXIn.com" ISBN="2-3631-4">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.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.CuoXIn.com" ISBN="2-3631-4">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.com</url>
</Website>
<Website genre="www.CuoXIn.com" ISBN="2-3631-4">
<title>武林網</title>
<author>作者</author>
<url>http://www.CuoXIn.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();

 

分享:ASP.Net中利用CSS實現多界面兩法
通過使頁面動態加載不同CSS實現多界面 (類似于這個blog)方法一: 以下為引用的內容:<%@page language="C#"%> <%@import namespace=&

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永顺县| 济源市| 石棉县| 田阳县| 龙川县| 高唐县| 邵阳县| 都安| 滨州市| 孟连| 沂源县| 新丰县| 广灵县| 咸宁市| 大余县| 乌兰浩特市| 桑日县| 江阴市| 德保县| 龙江县| 堆龙德庆县| 肃宁县| 乌海市| 新乡市| 汨罗市| 天水市| 房山区| 六盘水市| 固安县| 施秉县| 佳木斯市| 城市| 武邑县| 凤翔县| 石狮市| 固阳县| 忻城县| 阳山县| 西平县| 忻城县| 兴山县|