标签:
类名称 | 优点 | 缺点 |
XmlReader | 快速、高效、可扩展 | 只读,只向前,需要人工验证 |
XmlDocument | 可往返、可读写、支持XPath筛选 | 比XmlReader慢 |
XPathNavigator | 可往返,支持XPath和XSLT | 只读 |
XPathDocument | 比XmlDocument,优化支持XPath和XSLT | 比XmlReader慢 |
<?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"> //首发地址: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("<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>
标签:
原文地址:http://www.cnblogs.com/roboot/p/4772038.html