Using XmlReader to read two XML files

Source: Internet
Author: User
Tags cdata xpath xslt

In. NET development, it is often necessary to read and write XML files (app. Config and Web. config are a special column of the XML files used in WinForm and WebForm, and Microsoft provides a common approach, which we will not dwell on here). NET class Library provides a variety of ways to read and write XML files, each of which has its advantages and disadvantages, so it has its usefulness. The following is a list of read and write XML file classes provided by the Microsoft. NET class library and their characteristics:
Class name Advantages Disadvantages
XmlReader Fast, efficient, scalable Read only, forward only, requires manual verification
XmlDocument Can be round-trip, read-write, and support XPath filtering Slower than XmlReader
XPathNavigator Roundtrip, support for XPath and XSLT Read-only
XPathDocument Optimized support for XPath and XSLT than XmlDocument Slower than XmlReader
The XmlReader mentioned in this article is also a class in the Microsoft Class Library, which is characterized by fast, efficient and extensible, with the disadvantage of being read-only. The following examples illustrate the use of XmlReader:
(i) XML file used: Meu.xml
<?xml version="1.0"encoding="Utf-8"?><menus> <menu title="Common URLs"> <item name="World Network"Url="http://www.netskycn.com"Id="1"/> <item name="World Network Life Forum"Url="http://life.netskycn.com"Id="2"/> <item name="csdn"Url="http://www.csdn.net"Id="3"/> <item name="my blog"Url="HTTP://BLOG.CSDN.NET/ZHOUFOXCN"Id="4"/> <item name="Baidu"Url="http://www.baidu.com"Id="5"/> <item name="Google"Url="http://www.google.cn"Id="6"/> <item name="Microsoft"Url="http://www.microsoft.com"Id="7"/> </Menu> <menu title="Entertainment website"> <item name="Odd Tiger"Url="http://www.qihoo.com"Id=" A"/> <item name="NetEase"Url="http://www.163.com"Id=" -"/> <item name="Tianya"Url="http://www.tianya.cn"Id=" -"/> </Menu> <menu title="Secure URLs"> <item name=" the"Url="http://www.safe360.com"Id=" the"/> <item name="Rising"Url="http://www.rising.com.cn"Id=" -"/> </Menu></Menus>

(ii) Read and display: 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">"http://www.w3.org/1999/xhtml">"Content-type"Content="text/html; charset=gb2312"/><title>xmlreader instance </title>"Server">//Starting Address:http://blog.csdn.net/zhoufoxcn/archive/2007/12/23/1961624.aspxprotected voidPage_Load (Object Src, EventArgs E) {if(!IsPostBack) {XmlReader XmlReader= Xmlreader.create (Server.MapPath ("Menu.xml"));  while(Xmlreader.read ()) {Response.Write ("<li> node Type:"+ Xmlreader.nodetype +"==<br>"); Switch(xmlreader.nodetype) { Casexmlnodetype.xmldeclaration: for(inti =0; i < Xmlreader.attributecount; i++) {Xmlreader.movetoattribute (i); Response.Write ("Properties:"+ Xmlreader.name +"="+ xmlreader.value+"&nbsp;"); }                         Break;  CaseXmlnodetype.attribute: for(inti =0; i < Xmlreader.attributecount; i++) {Xmlreader.movetoattribute (i); Response.Write ("Properties:"+ Xmlreader.name +"="+ xmlreader.value+"&nbsp;"); }                         Break;  CaseXmlNodeType.CDATA:Response.Write ("CDATA:"+ xmlreader.value+"&nbsp;");  Break;  CaseXmlNodeType.Element:Response.Write ("node Name:"+ xmlreader.localname+"<br>");  for(inti =0; i < Xmlreader.attributecount; i++) {Xmlreader.movetoattribute (i); Response.Write ("Properties:"+ Xmlreader.name +"="+ xmlreader.value+"&nbsp;"); }                         Break;  CaseXmlNodeType.Comment:Response.Write ("Comment:"+xmlreader.value);  Break;  CaseXmlNodeType.Whitespace:Response.Write ("whitespace:"+"&nbsp;");  Break;  CaseXmlNodeType.ProcessingInstruction:Response.Write ("ProcessingInstruction:"+xmlreader.value);  Break;  CaseXmlNodeType.Text:Response.Write ("Text:"+xmlreader.value);  Break;  }} xmlreader.close (); }}</script></body>In particular, Menu.xml and readxml.aspx files are placed in the same folder, if the actual situation does not match, please change according to the actual situation. The following are the output results:
 
 
  • Node Type: xmldeclaration==
    Property: version=1.0 Property: Encoding=utf-8
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: Menus
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: Menu
    Properties: title= Common URLs
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Properties: Name= World Net Properties: Url=http://www.netskycn.com Properties: id=1
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Properties: Name= World Network Life Forum Properties: Url=http://life.netskycn.com Properties: id=2
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Property: Name=csdn Property: Url=http://www.csdn.net Property: id=3
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Properties: Name= My Blog properties: URL=HTTP://BLOG.CSDN.NET/ZHOUFOXCN properties: id=4
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Property: Name= Baidu Properties: Url=http://www.baidu.com Property: id=5
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Property: Name=google Property: url=http://www.google.cn Property: id=6
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Property: Name= Microsoft Properties: Url=http://www.microsoft.com Properties: id=7
  • Node Type: whitespace==
    Whitespace:
  • Node Type: endelement==
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: Menu
    Properties: title= Entertainment URL
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Properties: Name= Qihoo Properties: url=http://www.qihoo.com Properties: id=12
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Property: Name= NetEase Properties: url=http://www.163.com Properties: id=13
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Properties: Name= Tianya Properties: url=http://www.tianya.cn Properties: id=14
  • Node Type: whitespace==
    Whitespace:
  • Node Type: endelement==
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: Menu
    Properties: title= Security URL
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Property: Name=360 Property: Url=http://www.safe360.com Property: id=15
  • Node Type: whitespace==
    Whitespace:
  • Node Type: element==
    Node Name: item
    Property: Name= Rising property: url=http://www.rising.com.cn Property: id=16
  • Node Type: whitespace==
    Whitespace:
  • Node Type: endelement==
  • Node Type: whitespace==
    Whitespace:
  • Node Type: endelement==

Using XmlReader to read two XML files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.