How to Load XML Documents in ASP.NET 2.0 - Load (System.IO.Stream) method (Page 4 of 4 )
The class FileStream inherits from System.IO.Stream as shown in the next picture. This is utilized to read the file from the drive as shown in the code that follows.

Imports System.xml
Partial Class LoadStream
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim xmldoc As New XmlDocument
Dim fs As New IO.FileStream( _
"C:Inetpubwwwrootwebstudents.xml", _
IO.FileMode.Open, IO.FileAccess.Read)
xmldoc.Load(fs)
Response.Write(xmldoc.HasChildNodes)
Response.Write("<br/>")
Response.Write("What is the Last Child? " & "<b>" & _
xmldoc.DocumentElement.LastChild.InnerXml & "</b>")
End Sub
End Class
Again the DOM API is used to look at the Last child as in the code. The browser display for this is shown in the next picture. Although a file reference is passed as an argument, you may also pass a URL reference as in the Load (string) method discussed earlier.

Load (System.XML.XMLReader) method
The XMLReader is the API for parsing, and XMLTextReader, which inherits from the XMLReader, can be used for reading text or bytes.

Imports System.xml
Partial Class LoadXMLReader
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim xmldoc As New XmlDocument
Dim xmlrdr As New XmlTextReader( _
"http://localhost/webstudents.xml")
xmldoc.Load(xmlrdr)
Response.Write(xmldoc.HasChildNodes)
Response.Write("<br/>")
Response.Write("What is the Last Child? " & "<b>" & _
xmldoc.DocumentElement.LastChild.InnerXml & "</b>")
End Sub
End Class
Again the DOM API is used to look at the Last Child as in the code. The browser display for this is shown in the next picture. Although a URL reference is passed as an argument, you may also pass a File reference as in the Load (string) method discussed earlier.

Summary
This tutorial has shown with examples the several ways in which an XML document can be loaded. Once loaded it can be parsed, or utilized in several ways. All of the examples take the same webstudents.xml file used in several other tutorials. The examples are provided with ample reference to the various classes and the namespaces and should provide an appropriate starting point for those venturing into XML using VS 2005.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |