在.net开发中经常需要读写xml形式的文件(app.config和web.config分别是WinForm和WebForm中使用到的 xml文件的一个特列,并且微软提供了通用的方法,在此就不赘述了), .net类库提供了多种读写xml文件的方式,每一种方式都有其优点和 缺点,因而有其实用性。
下面列出微软.net类库提供的读写xml文件个类及其特点:
类名称 | 优点 | 缺点 |
XmlReader | 快速、高效、可扩展 | 只读,只向前,需要人工验证 |
XmlDocument | 可往返、可读写、支持XPath筛选 | 比XmlReader慢 |
XPathNavigator | 可往返,支持XPath和XSLT | 只读 |
XPathDocument | 比XmlDocument,优化支持XPath和XSLT | 比XmlReader慢 |
本文提到的XmlReader也是微软类库中的一个类,它的特点是快速高效,并且可扩展,缺点是只读。
下面举例说明XmlReader的用法:
(一)用到的xml文件:meu.xml
(一)用到的xml文件:meu.xml
xml version="1.0" encoding="utf-8"
?>
< Menus >
< Menu title ="常用网址" >
< item name ="天下网" url ="http://www.netskycn.com" id ="1" />
< item name ="天下网生活论坛" url ="http://life.netskycn.com" id ="2" />
< item name ="csdn" url ="http://www.csdn.net" id ="3" />
< item name ="我的博客" url ="http://blog.csdn.net/zhoufoxcn" id ="4" />
< item name ="百度" url ="http://www.baidu.com" id ="5" />
< item name ="Google" url ="http://www.google.cn" id ="6" />
< item name ="微软" url ="http://www.microsoft.com" id ="7" />
Menu >
< Menu title ="娱乐网址" >
< item name ="奇虎" url ="http://www.qihoo.com" id ="12" />
< item name ="网易" url ="http://www.163.com" id ="13" />
< item name ="天涯" url ="http://www.tianya.cn" id ="14" />
Menu >
< Menu title ="安全网址" >
< item name ="360" url ="http://www.safe360.com" id ="15" />
< item name ="瑞星" url ="http://www.rising.com.cn" id ="16" />
Menu >
Menus >
< Menus >
< Menu title ="常用网址" >
< item name ="天下网" url ="http://www.netskycn.com" id ="1" />
< item name ="天下网生活论坛" url ="http://life.netskycn.com" id ="2" />
< item name ="csdn" url ="http://www.csdn.net" id ="3" />
< item name ="我的博客" url ="http://blog.csdn.net/zhoufoxcn" id ="4" />
< item name ="百度" url ="http://www.baidu.com" id ="5" />
< item name ="Google" url ="http://www.google.cn" id ="6" />
< item name ="微软" url ="http://www.microsoft.com" id ="7" />
Menu >
< Menu title ="娱乐网址" >
< item name ="奇虎" url ="http://www.qihoo.com" id ="12" />
< item name ="网易" url ="http://www.163.com" id ="13" />
< item name ="天涯" url ="http://www.tianya.cn" id ="14" />
Menu >
< Menu title ="安全网址" >
< item name ="360" url ="http://www.safe360.com" id ="15" />
< item name ="瑞星" url ="http://www.rising.com.cn" id ="16" />
Menu >
Menus >
<%
@ Page Language
=
"
C#
"
ContentType
=
"
text/html
"
ResponseEncoding
=
"
gb2312
"
%>
<% @ Import Namespace = " System.Xml " %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > XMLReader实例 title >
head >
< body >
< script runat ="server" >
<% @ Import Namespace = " System.Xml " %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > XMLReader实例 title >
head >
< body >
< script runat ="server" >
//首发地址:http://blog.csdn.net/zhoufoxcn/archive/2007/12/23/1961624.aspx
protected void Page_Load(Object Src, EventArgs E)
{
if ( ! IsPostBack)
{
XmlReader xmlReader = XmlReader.Create(Server.MapPath( " Menu.xml " ));
while (xmlReader.Read())
{
Response.Write( "节点类型:
"
+
xmlReader.NodeType
+
"
==
" );
switch (xmlReader.NodeType)
{
case XmlNodeType.XmlDeclaration:
for ( int i = 0 ; i < xmlReader.AttributeCount; i ++ )
{
xmlReader.MoveToAttribute(i);
Response.Write( " 属性: " + xmlReader.Name + " = " + xmlReader.Value + " " );
}
break ;
case XmlNodeType.Attribute:
for ( int i = 0 ; i < xmlReader.AttributeCount; i ++ )
{
xmlReader.MoveToAttribute(i);
Response.Write( " 属性: " + xmlReader.Name + " = " + xmlReader.Value + " " );
}
break ;
case XmlNodeType.CDATA:
Response.Write( " CDATA: " + xmlReader.Value + " " );
break ;
case XmlNodeType.Element:
Response.Write( " 节点名称: " + xmlReader.LocalName + "
" );
for ( int i = 0 ; i < xmlReader.AttributeCount; i ++ )
{
xmlReader.MoveToAttribute(i);
Response.Write( " 属性: " + xmlReader.Name + " = " + xmlReader.Value + " " );
}
break ;
case XmlNodeType.Comment:
Response.Write( " Comment: " + xmlReader.Value);
break ;
case XmlNodeType.Whitespace:
Response.Write( " Whitespace: " + " " );
break ;
case XmlNodeType.ProcessingInstruction:
Response.Write( " ProcessingInstruction: " + xmlReader.Value);
break ;
case XmlNodeType.Text:
Response.Write( " Text: " + xmlReader.Value);
break ;
}
}
xmlReader.Close();
}
}
script >
body >
html >
protected void Page_Load(Object Src, EventArgs E)
{
if ( ! IsPostBack)
{
XmlReader xmlReader = XmlReader.Create(Server.MapPath( " Menu.xml " ));
while (xmlReader.Read())
{
Response.Write( "
" );
switch (xmlReader.NodeType)
{
case XmlNodeType.XmlDeclaration:
for ( int i = 0 ; i < xmlReader.AttributeCount; i ++ )
{
xmlReader.MoveToAttribute(i);
Response.Write( " 属性: " + xmlReader.Name + " = " + xmlReader.Value + " " );
}
break ;
case XmlNodeType.Attribute:
for ( int i = 0 ; i < xmlReader.AttributeCount; i ++ )
{
xmlReader.MoveToAttribute(i);
Response.Write( " 属性: " + xmlReader.Name + " = " + xmlReader.Value + " " );
}
break ;
case XmlNodeType.CDATA:
Response.Write( " CDATA: " + xmlReader.Value + " " );
break ;
case XmlNodeType.Element:
Response.Write( " 节点名称: " + xmlReader.LocalName + "
" );
for ( int i = 0 ; i < xmlReader.AttributeCount; i ++ )
{
xmlReader.MoveToAttribute(i);
Response.Write( " 属性: " + xmlReader.Name + " = " + xmlReader.Value + " " );
}
break ;
case XmlNodeType.Comment:
Response.Write( " Comment: " + xmlReader.Value);
break ;
case XmlNodeType.Whitespace:
Response.Write( " Whitespace: " + " " );
break ;
case XmlNodeType.ProcessingInstruction:
Response.Write( " ProcessingInstruction: " + xmlReader.Value);
break ;
case XmlNodeType.Text:
Response.Write( " Text: " + xmlReader.Value);
break ;
}
}
xmlReader.Close();
}
}
script >
body >
html >
特别说明的是:menu.xml和ReadXml.aspx文件放在同一个文件夹下,如果实际情况与此不符,请根据实际情况更改。
以下是输出结果:
属性:version=1.0 属性:encoding=utf-8
Whitespace:
节点名称:Menus
Whitespace:
节点名称:Menu
属性:title=常用网址
Whitespace:
节点名称:item
属性:name=天下网 属性:url=http://www.netskycn.com 属性:id=1
Whitespace:
节点名称:item
属性:name=天下网生活论坛 属性:url=http://life.netskycn.com 属性:id=2
Whitespace:
节点名称:item
属性:name=csdn 属性:url=http://www.csdn.net 属性:id=3
Whitespace:
节点名称:item
属性:name=我的博客 属性:url=http://blog.csdn.net/zhoufoxcn 属性:id=4
Whitespace:
节点名称:item
属性:name=百度 属性:url=http://www.baidu.com 属性:id=5
Whitespace:
节点名称:item
属性:name=Google 属性:url=http://www.google.cn 属性:id=6
Whitespace:
节点名称:item
属性:name=微软 属性:url=http://www.microsoft.com 属性:id=7
Whitespace:
Whitespace:
节点名称:Menu
属性:title=娱乐网址
Whitespace:
节点名称:item
属性:name=奇虎 属性:url=http://www.qihoo.com 属性:id=12
Whitespace:
节点名称:item
属性:name=网易 属性:url=http://www.163.com 属性:id=13
Whitespace:
节点名称:item
属性:name=天涯 属性:url=http://www.tianya.cn 属性:id=14
Whitespace:
Whitespace:
节点名称:Menu
属性:title=安全网址
Whitespace:
节点名称:item
属性:name=360 属性:url=http://www.safe360.com 属性:id=15
Whitespace:
节点名称:item
属性:name=瑞星 属性:url=http://www.rising.com.cn 属性:id=16
Whitespace:
Whitespace: