XML integration with the Microsoft .Net Framework 2.0 provides the XMLDocument class for working with XML. Loading an XML document, a task needed in many web applications, can be accomplished using several forms of the Load() method. The article introduces you to several of these load methods with concrete examples providing you with the background necessary to use these methods.
The next picture shows you the Object Browser showing the XMLDocument class in the System.XML namespace. The XMLDocument class inherits from the XMLNode class as in the following hierarchical relationship:
The XMLDocument class has a large number of Public/Private properties and methods shown in the picture, which are also from the object browser in the Visual Studio 2.0 IDE. This class can invoke its methods not only to load XML documents but also create XML documents with all the different element types you would find in an XML document. This article however will look only at loading the XML document. We will assume it exists as an xml file on the hard drive of the machine; or is available as a file that can be accessed as an URL; or that the content is available as a string.
The five different Load() methods shown in the above picture surrounded by a red rectangle will be explored with code to show how each of them may be used to load the XMLDocument. The various methods are used in a web site project, XMLSite. The XMLDocument gets instantiated and loaded in the click event of a button. All of the examples use the same XML file which has been used in other XML related tutorials by the author on the DevArticles.com and ASPFree.com web sites.
LoadXml (string) method
After instantiating an XMLDocument using the New keyword you can invoke the LoadXml() method with the XML file in a string format as an argument as shown in the following picture. Also observe that the argument to the method typed in is an XML file content in a string format.
Once the XML document is loaded you can invoke its properties through the XML DOM Model to verify if the content is loaded correctly. This may be simply tested as shown by the completed code and the output written as a Response.Write() as shown.
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.
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.