How to Load XML Documents in ASP.NET 2.0 - Load (string) where string is an XML file
(Page 3 of 4 )
The Load (string) method takes as an argument the reference to an XML file located in the file system as shown in the following code.
Imports system.xml
Partial Class LoadString
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
xmldoc.Load("C:Inetpubwwwrootwebstudents.xml")
Response.Write("Loaded as an xml file located on the C drive<br/>")
Response.Write("Does the document has child nodes? " _
& "<b>" & xmldoc.HasChildNodes & "</b>")
Response.Write("<br/>")
Response.Write("What is the Last child? " & "<b>" _
& xmldoc.DocumentElement.LastChild.InnerXml & "</b>")
Response.Write("<br/>")
Response.Write("What is Last child's Last child? " & _
"<b>" & xmldoc.DocumentElement.LastChild.LastChild.InnerXml _
& "</b>")
End Sub
End Class
Again the DOM API is used to look at the Last child and the Last child's Last child as in the code. The browser display for this is shown in the next picture.

Load (System.IO.TextReader) method
The next picture shows the System.IO.TextReader class. The Read() method of this will be invoked to load the xml as in the code.

Imports System.xml
Partial Class LoadWithTextReader
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim docxml As New XmlDocument
Dim rdr As New XmlTextReader( _
"http://localhost/webstudents.xml")
rdr.Read()
docxml.Load(rdr)
Response.Write("What is the Last Child? " & "<b>" & _
docxml.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 is passed as an argument, you may also pass a file reference as in the Load (string) method discussed earlier.

It may be noted that the StreamReader class can also be used to read the XML from the file as a text from its Read() method since the StreamReader inherits from the System.IO.TextReader as shown in the next picture.

Next: Load (System.IO.Stream) method >>
More ASP.NET Code Articles
More By Jayaram Krishnaswamy