关闭顶部展开顶部

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

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐: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.mb5u.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.mb5u.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.mb5u.com" ISBN="2-3631-4">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.com</url>
</Website>
<Website genre="www.mb5u.com" ISBN="2-3631-4">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.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.mb5u.com" ISBN="2-3631-4">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.com</url>
</Website>
<Website genre="www.mb5u.com" ISBN="2-3631-4">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.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.mb5u.com" ISBN="2-3631-4">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.com</url>
</Website>
<Website genre="www.mb5u.com" ISBN="2-3631-4">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.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.mb5u.com" ISBN="2-3631-4" test="99999">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.com</url>
<fffff>1</fffff>
</Website>
<Website genre="www.mb5u.com" ISBN="2-3631-4" test="99999">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.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.mb5u.com</url>
</Website>
<Website ISBN="2-3631-4" test="99999">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.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.mb5u.com" ISBN="2-3631-4">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.com</url>
</Website>
<Website genre="www.mb5u.com" ISBN="2-3631-4">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.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.mb5u.com" ISBN="2-3631-4">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.com</url>
</Website>
<Website genre="www.mb5u.com" ISBN="2-3631-4">
<title>模板无忧</title>
<author>作者</author>
<url>http://www.mb5u.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=&

来源:模板无忧//所属分类:.Net教程/更新时间:2008-08-22
loading.. 评论加载中....
相关.Net教程
闂佹眹鍩勯崹閬嶆偤閺囶澁缍栭柛鈩冪⊕閳锋帗銇勯弴妤€浜惧銈忕秶閹凤拷
濠电偛顕慨顓㈠磻閹炬枼妲堥柡鍌濇硶婢ф稒淇婇懠顒夆偓婵嬫煟閵忊晛鐏查柟鍑ゆ嫹
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓兼径瀣閻庣懓瀚竟鍡欐崲娑斾線鏌i姀鈺佺伈闁瑰嚖鎷�
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓兼径濠勵吋闂佽鍨庨崟顓фК闂佽閰eḿ褍螞濞戙垺鍋夐柨鐕傛嫹
闂備胶枪缁绘劙骞婃惔銊ョ劦妞ゆ帒鍊哥敮鍫曞箹鐎涙ḿ鐭掔€规洘绻堥弫鎾绘晸閿燂拷
闂備胶枪缁绘劙骞婃惔銊ョ劦妞ゆ巻鍋撻柛姘儑缁﹪鏁傞崜褏鐓撻柣搴岛閺呮繈鎯屽▎鎴犵=濞撴艾锕ョ€氾拷
闂備浇銆€閸嬫挻銇勯弽銊р槈闁伙富鍣i弻娑樷攽閹邦亞鑳虹紓浣靛妽濡炶棄顕i妸鈺婃晬婵炲棙鍨电粭锟犳⒑閸濆嫬鈧骞婇幘鑸殿潟闁跨噦鎷�
闂備礁鎼崯鐗堟叏妞嬪海绀婂鑸靛姈閻擄綁鎮规潪鎷岊劅婵炲眰鍊曢湁闁挎繂妫欑粈鈧梺鍛娚戦悧鐘茬暦閹扮増鏅搁柨鐕傛嫹
婵犵妲呴崹顏堝礈濠靛棭鐔嗘俊顖氬悑鐎氱粯銇勯幘瀵哥畺閻庢熬鎷�
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓奸崶銊ヮ伕濡炪倖鎸荤换鍐偓姘虫珪娣囧﹪顢涘Δ鈧晶鍙夌節椤喗瀚�
婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾鐎规洦鍓熼、娆撳礂閻撳簶鍋撻悽鍛婄厸闁割偅绻勫瓭婵犳鍣幏锟�
婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾闁哄苯鎳橀崺鈧い鎺嗗亾闁宠閰i獮鎴﹀箛闂堟稒顔嗛梻浣告惈鐎氭悂骞忛敓锟�
婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾鐎规洩缍侀獮瀣攽閸偂绱�
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓兼径濠勭厬闂佺懓鐡ㄧ换鍕敂鐎涙ü绻嗘い鏍殔婢у弶绻濋~顔藉
闂佽楠搁崢婊堝礈濠靛鍋嬮柟鎯版閻鈹戦悩鎻掓殭闁奸潧缍婇弻銈夋嚍閵夈儱顫嶉梺缁樼壄缂嶄礁鐣峰▎鎾存櫢闁跨噦鎷�
UB闂備礁婀辩划顖炲礉濡ゅ懐宓侀柛銉㈡櫆鐎氭岸鎮楀☉娅虫垿锝為敓锟�
闂備浇澹堟ご绋款潖婵犳碍鐒鹃悗鐢电《閸嬫捇鐛崹顔句痪濠电姭鍋撻柨鐕傛嫹
闂佽楠哥粻宥夊垂閸濆嫸鑰块柛銏㈠殰
闂備礁鎲″缁樻叏妞嬪海绀婂璺虹灱閸楁碍绻涢崱妤€顒㈤柛鐐差槹缁绘稓绱欓悩鍝勫帯闂佺ǹ楠忛幏锟�
缂傚倸鍊烽悞锕傛偡閿曞倸鍨傛繝濠傚椤╅攱銇勯幒宥囶槮缂佹彃婀遍埀顒傚仯閸婃繄绱撳棰濇晩闁跨噦鎷�
©2017 www.mb5u.com婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾鐎殿噮鍣i幃鈺呭箵閹烘挸鐦�
闂備浇銆€閸嬫捇鏌熼婊冾暭妞ゃ儻鎷�&闂備礁鎲$敮鎺懳涢弮鍫燁棅闁跨噦鎷�