E:\web\learnaspcom\htdocs\freebook\learn\ubtoc.xml LearnAsp.com - ASP ASP.net Free Lessons
Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
xxx

Using XMLTextReader to Read XML Data
by Charles Carroll

The XMLTextReader can be much easier than the DOM or SAX to locate and filter data. It is ideal for large files since unlike the DOM it never loads an entire file in memory. Its design is so you can read through the file and when you notice items you want to grab you respond appropriately.

Here we make a simple program to grab all the <quote>...</quote> element values and the ISBN of the books they are attached to.

   filename=/experiments/xmltextreader/quotefinder.aspx

<Test Script Below>


<%@trace="true" debug="true" %>
<%@ Import Namespace="System.XML"%>
<%@ Import Namespace="system.xml.xmlnodetype"%>
<script language="VB" runat="server">
Sub Page_Load(S As Object, E As EventArgs)
    trace.tracemode=TraceMode.SortByTime
    
    DIM Xmltr1 as XMLTextReader
    
    TRY
        Xmltr1=new XMLtextReader(server.mappath("/experiments/data/favebooks.xml"))
        DIM strISBN as string

    
        DO WHILE Xmltr1.Read()
            IF XmlTr1.nodeType=Element AND Xmltr1.Name="isbn"
                Xmltr1.Read()
                strISBN=XmlTr1.Value            
            END IF        
            IF XmlTr1.nodeType=Element AND Xmltr1.Name="quote"
                trace.write("ISBN=", strISBN)
                Xmltr1.Read()
                trace.write("Quote=",XmlTr1.value)            
            END IF
        LOOP
    
    CATCH ex1 as exception
    
    FINALLY
        Xmltr1.Close()
        
    END TRY
End Sub

</script>
<html><head>
<title>XML Demo</title>
</head>
<body bgcolor="#FFFFFF">
<form runat="server">
<ASP:placeholder id="plcErr" runat="server" />
See The Trace For More Details
</form>
</body></html>

Send Us a Holiday Gift!. Charles celebrates Christmas, Jewish holidays, Kwanza, Chinese New Year, Japanese Holidays, Secretary Day, High Muslim Holy Days, Pagan & Wicca holidays, and many more! - send a gift any HOLIDAY. 2004 gift gallery & 2005 gift gallery