|
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
<%@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>
|