search
HomeBackend DevelopmentXML/RSS TutorialDetailed introduction to the code example of using XmlReader to read xml files

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:

##XmlReaderFast, efficient, scalableRead-only, forward-only, manual verification requiredXmlDocument Round trip, read and write, supportSlower than XmlReaderXPathNavigator Can round trip, supports XPath and only ReadXPathDocument than XmlDocument, optimized to support XPath and XSLTSlower than XmlReaderThe XmlReader mentioned in this article is also a class in the Microsoft class library. It is characterized by being fast, efficient, and scalable. The disadvantages are Read only.

Class name

Advantages

##Disadvantages

XPath

Filtering

XSLT

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>

(2) Read and display: ReadXml.aspx

<?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!

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    RSS in XML: Unveiling the Core of Content SyndicationRSS in XML: Unveiling the Core of Content SyndicationApr 22, 2025 am 12:08 AM

    The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

    Beyond the Basics: Advanced RSS Document FeaturesBeyond the Basics: Advanced RSS Document FeaturesApr 21, 2025 am 12:03 AM

    Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

    The XML Backbone: How RSS Feeds are StructuredThe XML Backbone: How RSS Feeds are StructuredApr 20, 2025 am 12:02 AM

    RSSfeedsuseXMLtostructurecontentupdates.1)XMLprovidesahierarchicalstructurefordata.2)Theelementdefinesthefeed'sidentityandcontainselements.3)elementsrepresentindividualcontentpieces.4)RSSisextensible,allowingcustomelements.5)Bestpracticesincludeusing

    RSS & XML: Understanding the Dynamic Duo of Web ContentRSS & XML: Understanding the Dynamic Duo of Web ContentApr 19, 2025 am 12:03 AM

    RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

    RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

    RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

    Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

    The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

    How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

    RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

    RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

    RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor