In .net development, it is often necessary to read and write files in the form of xml (app.config and web.config are special columns of xml files used in WinForm and WebForm respectively, and Microsoft provides Common methods, which will not be described here), .netClass Library provides a variety of ways to read and write xml files, each method has its advantages and disadvantages, and therefore has its practicality.
The following is a list of classes and their characteristics for reading and writing xml files provided by the Microsoft .net class library:
Class name |
Advantages |
##Disadvantages |
Fast, efficient, scalable | Read-only, forward-only, manual verification required | |
Round trip, read and write, support | XPath | Slower than XmlReader|
Can round trip, supports XPath and | XSLT | only Read|
than XmlDocument, optimized to support XPath and XSLT | Slower than XmlReader |
The following examples illustrate the usage of XmlReader:
(1) XML file used: 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://l if e.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>
<?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> (二)读取并显示:ReadXml.aspx <%@ 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"> protected void Page_Load(Object Src, EventArgs E) { if (!IsPostBack) { XmlReader xmlReader = XmlReader.Create(Server.MapPath("Menu.xml")); while (xmlReader.Read()) { Response.Write("<li>节点类型:" + xmlReader.NodeType + "==<br>"); 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+"<br>"); 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>
Special instructions are: the menu.xml and ReadXml.aspx files are placed in the same folder. If the actual situation is not consistent with this, please change it according to the actual situation.
The following is the output:
节点类型:XmlDeclaration==属性:version=1.0 属性:encoding=utf-8 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:Menus 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:Menu属性:title=常用网址 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=天下网 属性:url=http://www.netskycn.com 属性:id=1 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=天下网生活论坛 属性:url=http://life.netskycn.com 属性:id=2 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=csdn 属性:url=http://www.csdn.net 属性:id=3 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=我的博客 属性:url=http://blog.csdn.net/zhoufoxcn 属性:id=4 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=百度 属性:url=http://www.baidu.com 属性:id=5 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=Google 属性:url=http://www.google.cn 属性:id=6 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=微软 属性:url=http://www.microsoft.com 属性:id=7 节点类型:Whitespace==Whitespace: 节点类型:EndElement== 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:Menu属性:title=娱乐网址 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=奇虎 属性:url=http://www.qihoo.com 属性:id=12 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=网易 属性:url=http://www.163.com 属性:id=13 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=天涯 属性:url=http://www.tianya.cn 属性:id=14 节点类型:Whitespace==Whitespace: 节点类型:EndElement== 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:Menu属性:title=安全网址 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=360 属性:url=http://www.safe360.com 属性:id=15 节点类型:Whitespace==Whitespace: 节点类型:Element==节点名称:item属性:name=瑞星 属性:url=http://www.rising.com.cn 属性:id=16 节点类型:Whitespace==Whitespace: 节点类型:EndElement== 节点类型:Whitespace==Whitespace: 节点类型:EndElement==
The above is the detailed content of Detailed introduction to the code example of using XmlReader to read xml files. For more information, please follow other related articles on the PHP Chinese website!