Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » ASP.NET/Web Applications »
Differences Between XMLDOM and .NET XML
|
Differences Between XMLDOM and .NET XML
XML documents have become a great way to pass data from one application to another. Whether communicating from one DLL to another, from one EXE to another, or even from one server to another, XML is simple, easy, and efficient to pass around. In Microsoft Visual Basic 6.0, we used the Microsoft.XMLDOM object to process XML documents. In Microsoft .NET, the equivalent object is called System.Xml.XmlDocument. In this chapter, we will learn how to work with XML documents within the Microsoft .NET Framework. In addition, we will also see how these objects differ between Visual Basic 6.0 and Visual Basic .NET.
The XML Parser
A .NET application inputs XML from a variety of sources. A client application transmits parameters to a server via an XML document. An application server parses and processes the content of the XML document. After completion of the server process, the client receives an XML document. In the past, you have relied on the Microsoft.XMLDOM component to process XML documents. This component is commonly invoked from Active Server Pages, Visual Basic, and Internet Explorer. Fortunately, the .NET Framework provides a high-performance object that you can employ to parse and process your XML documents.
In this chapter, we will learn how to load an XML document into .NET and display its content. We will also see comparisons between the new System.Xml and the old Microsoft.XMLDOM. These comparisons provide a clear understanding of how XML works in the .NET Framework. The following figure shows the XML document that we will be using throughout this chapter.
Loading an XML Document
When we load and parse an XML document, we create an instance of an object library that knows how to load and parse the document. After the object has been instantiated, we can use various commands to process its contents. The following are the steps that we typically follow in the SCT Matrix application.
1. Instantiate the XML parser.
2. Load a specific XML document.
3. Display the data located in the XML document.
Loading an XML in Visual Basic 6.0
The Microsoft.XMLDOM object can be instantiated from Visual Basic, Active Server Pages, or Internet Explorer 5.x and above. This first example code demonstrates how to instantiate the XMLDOM component in Visual Basic. Once the object is ready, we can load an XML file. This file can then be displayed to the user.
Private Sub btnLoad_Click()
Set MyXMLDOM = CreateObject("Microsoft.XMLDOM")
MyXMLDOM.Load ("Contact.xml")
MsgBox MyXMLDOM.xml
End Sub
First, the example instantiates a Microsoft.XMLDOM object using the CreateObject function. This creates the XMLDOM object and places a reference to this new object in the variable MyXMLDOM. You then use the Load method of the XMLDOM object to load an XML file from disk. Finally, we can display the XML by accessing the XML property of the XMLDOM document object.
Loading an XML in .NET
In Visual Basic .NET, we instantiate the XML document provided with the .NET Framework. The object models used here will be the same for any .NET-friendly language, including Visual Basic .NET. The following example shows how a System.Xml.XmlDocument object can be instantiated.
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnLoad.Click
Dim MyXMLDocument As System.Xml.XmlDocument
MyXMLDocument = New System.Xml.XmlDocument()
MyXMLDocument.Load("Contact.xml")
MessageBox.Show(MyXMLDocument.InnerXml)
MessageBox.Show(MyXMLDocument.InnerText)
End Sub
One of the syntactical changes in .NET is in the way in which we reference the content of the document. Rather than reference the XML property, we now reference the InnerXml property. This property returns the complete XML document. To display the data without the XML tags, we use the InnerText property. Note that the System.Xml reference in the above code sample can be removed by importing the System.Xml namespace.
XPATH
XPath provides a query language for XML documents just like Structure Query Language (SQL) provides a query language for relational databases. The XPath vocabulary allows us to select specific nodes that meet specific criteria. When we open an XML document in .NET, we might have to frequently execute XPath queries to retrieve a subset of the nodes contained in the document. Consider a document that contains a client contact, as shown in the following listing.
John
Smith
1 Main Street
Apt. #5
www.johnsmith.com
john@smith.com
John.Smith@smith.com
To retrieve the data within the Address node, we can specify the following XPath query: /contact/address.
The following are the steps that we normally use in our code to select nodes matching a specific criteria and then process them:
1. Instantiate the XML parser.
2. Load a specific XML document.
3. Define the XPath.
4. Select the element that matches the XPath.
5. Process the matching element.
XPath with Visual Basic 6.0
After an XML document is loaded, we can apply an XPath to retrieve a subset of elements contained within the document. This involves executing the SelectSingleNode method of the XMLDOM object. This method returns the single node that matches the criteria as shown in the following example.
Private Sub btnSelect_Click()
Set MyXMLDOM = CreateObject("Microsoft.XMLDOM")
MyXMLDOM.Load ("Contact.xml")
MyXpath = "/contact/name/first"
Set MyNode = MyXMLDOM.selectSingleNode(MyXpath)
MsgBox MyNode.xml
End Sub
Notice that the content of the XML property of the Node object is the first name node alone. The parent nodes are not included. If this element contained children, these children would be contained within the Node object.
XPath with .NET
The process of applying XPath in Visual Basic .NET is consistent with that of Visual Basic 6.0. Once the XML document has been loaded, we can retrieve a specific child element with an XPath expression as shown in the following example.
Private Sub btnSelectSingleNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnSelectSingleNode.Click
Dim MyXMLDocument As System.Xml.XmlDocument
MyXMLDocument = New System.Xml.XmlDocument()
MyXMLDocument.Load("Contact.xml")
Dim MyNode As System.Xml.XmlNode
MyNode = MyXMLDocument.SelectSingleNode(“/contact/name/first”)
MessageBox.Show(MyNode.OuterXml)
MessageBox.Show(MyNode.InnerXml)
End Sub
The difference between the XML property in XMLDOM and the InnerXml property in System.Xml becomes apparent when we select a single node with XPath. The content of InnerXml contains the element content without the element tag name. If we are looking for both the content and the tag name, we have to use the OuterXml property.
Working with Node Lists
In the preceding examples, we retrieved just one node using an XPath query. What if the XPath matches multiple nodes? It’s possible to retrieve more than one matching node. Simply replace the SelectSingleNode() method with the SelectNodes() method when we expect the XPath to match against more than one node. The return value of the SelectNodes() method is a NodeList, which is an array of nodes. The following are the steps that we need to follow to work with node lists.
1. Instantiate the XML parser.
2. Load a specific XML document.
3. Define the XPath.
4. Select the elements that match the XPath.
5. Iterate through all the matching nodes.
Node Lists in Visual Basic 6.0
Here is the code that we typically use for working with node lists.
Private Sub btnSelectNodes_Click()
Set MyXMLDOM = CreateObject("Microsoft.XMLDOM")
MyXMLDOM.Load ("Contact.xml")
MyXpath = "/contact/email"
Set MyNodeList = MyXMLDOM.selectNodes(MyXpath)
MsgBox MyNodeList.length
For x = 0 To MyNodeList.length - 1
MsgBox MyNodeList.Item(x).xml
Next
End Sub
The Contact XML document contains more than one email address. When we select these email address nodes, we get a NodeList containing each address. The Length property of the NodeList indicates how many nodes were returned from the XPath statement. Use this value to determine the upper bounds of the FOR loop. As we iterate through each member of the NodeList, we can process each email address node separately.
Node Lists in .NET
Working with a NodeList in System.Xml is similar to XMLDOM. The method calls are the same. The object names are the same. But the property used to determine the quantity of matching elements is called Count. Here is the same code in .NET.
Private Sub btnSelectNodes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnSelectNodes.Click
Dim MyXMLDocument As System.Xml.XmlDocument
MyXMLDocument = New System.Xml.XmlDocument()
MyXMLDocument.Load("Contact.xml")
Dim MyXpath As String
MyXpath = "/contact/email"
Dim MyNodeList As System.Xml.XmlNodeList
MyNodeList = MyXMLDocument.SelectNodes(MyXpath)
MessageBox.Show(MyNodeList.Count)
Dim x As Integer
For x = 0 To MyNodeList.Count - 1
MessageBox.Show(MyNodeList.Item(x).InnerXml)
Next
End Sub
The Count property indicates the number of matching Nodes in the NodesList. Remember that the NodeList item index starts at position zero. This means that the quantity minus one is the upper bound of the array.
Reading Attributes
XML documents structure data into elements and attributes. In the XML sample that we are using throughout, the city, state, and zip are included as attributes of the Address element. In this section we will see how to read these attributes programmatically.
Reading Attributes in Visual Basic 6.0
The following is the code that we write in Visual Basic to access attributes present in an XML document.
Private Sub btnReadAttribute_Click()
Set MyXMLDOM = CreateObject("Microsoft.XMLDOM")
MyXMLDOM.async = False
MyXMLDOM.Load ("c:\DotNetJumpstart\Contact.xml")
MyXpath = "/contact/address/@city"
Set MyNode = MyXMLDOM.selectSingleNode(MyXpath)
MsgBox (MyNode.Text)
End Sub
The XPath vocabulary distinguishes attributes from elements by prefixing the node name with the @ character. To reference the city attribute of the address node, the XPath will look like this: /contact/address/@city.
Reading attributes in .NET
We can reference attributes in .NET the same way as in Visual Basic 6.0. Simply supply an XPath query that references the attribute of an element, which includes prefixing the attribute name with an @ character in the XPath as shown in the following example.
Private Sub btnReadAttribute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnReadAttribute.Click
Dim MyXMLDocument As System.Xml.XmlDocument
MyXMLDocument = New System.Xml.XmlDocument()
MyXMLDocument.Load("c:\DotNetJumpstart\Contact.xml")
Dim MyXpath As String
MyXpath = "/contact/address/@city"
Dim MyNode As System.Xml.XmlNode
MyNode = MyXMLDocument.SelectSingleNode(MyXpath)
MessageBox.Show(MyNode.OuterXml)
MessageBox.Show(MyNode.InnerXml)
End Sub
To access an attribute from XPath, we prefix the attribute name with the @ sign to indicate that we are looking for an attribute name. When this node is selected, we can view the InnerXml to see the value of the attribute. If we access the OuterXml property, we receive the attribute name and attribute value together. Generally, we will not need this combination of information during processing. InnerXml is all that we need to use.
Updating an element
XML documents are updateable and the .NET application is able to modify the content of an XML document. The new information may come from a user interface form or other device. In this section we will see how to update the value of an element.
Updating an element in Visual Basic 6.0
The following example shows how we can update an element in Visual Basic 6.0 using the XML DOM.
Private Sub btnUpdateElement_Click()
Set MyXMLDOM = CreateObject("Microsoft.XMLDOM")
MyXMLDOM.Load ("Contact.xml")
MsgBox MyXMLDOM.xml
MyXpath = "/contact/name/first"
Set MyNode = MyXMLDOM.selectSingleNode(MyXpath)
MsgBox MyNode.xml
MyNode.Text = "Roberto"
MsgBox MyNode.xml
MsgBox MyXMLDOM.xml
End Sub
In order to modify the content of a single element, we need to select that single element. Once we have a node object that points to this element, its content can be modified by using the Text property. This is a read/write property.
Updating an element in .NET
The process of modifying an element with System.Xml is similar to XMLDOM, but the property names are different. Rather than referencing the Text property, we will have to reference the InnerText property as shown in the following example.
Private Sub btnUpdateElement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnUpdateElement.Click
Dim MyXMLDocument As System.Xml.XmlDocument
MyXMLDocument = New System.Xml.XmlDocument()
MyXMLDocument.Load("Contact.xml")
MessageBox.Show(MyXMLDocument.InnerXml)
Dim MyXpath As String
MyXpath = "/contact/name/first"
Dim MyNode As System.Xml.XmlNode
MyNode = MyXMLDocument.SelectSingleNode(MyXpath)
MessageBox.Show(MyNode.OuterXml)
MyNode.InnerText = "Roberto"
MessageBox.Show(MyNode.OuterXml)
MessageBox.Show(MyXMLDocument.InnerXml)
End Sub
Updating an Attribute
In an earlier section we saw how to update an element in an XML document. In this section we will see how to update attributes that are present in the XML document.
Updating an Attribute in Visual Basic 6.0
In order to update an attribute, we need to select the attribute and then update it as shown in the following example.
Private Sub btnUpdateAttribute_Click()
Set MyXMLDOM = CreateObject("Microsoft.XMLDOM")
MyXMLDOM.async = False
MyXMLDOM.Load ("c:\DotNetJumpstart\Contact.xml")
MsgBox (MyXMLDOM.xml)
MyXpath = "/contact/address/@city"
Set MyNode = MyXMLDOM.selectSingleNode(MyXpath)
MsgBox (MyNode.Text)
MyNode.Text = "Lake Forest"
MsgBox (MyNode.Text)
MsgBox (MyXMLDOM.xml)
End Sub
Updating an Attribute in .NET
In .NET we can update the content of an attribute in virtually the same way as we did in Visual Basic 6.0. First, we select the attribute using XPath as demonstrated above. After selecting the node we can supply its content. The main difference between .NET and Visual Basic 6.0 is that we will reference the InnerText property in .NET rather than the Text property as in Visual Basic 6.0. The following example shows an attribute update in .NET.
Private Sub btnUpdateAttribute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnUpdateAttribute.Click
Dim MyXMLDocument As System.Xml.XmlDocument
MyXMLDocument = New System.Xml.XmlDocument()
MyXMLDocument.Load("Contact.xml")
MessageBox.Show(MyXMLDocument.InnerXml)
Dim MyXpath As String
MyXpath = "/contact/address/@city"
Dim MyNode As System.Xml.XmlNode
MyNode = MyXMLDocument.SelectSingleNode(MyXpath)
MessageBox.Show(MyNode.OuterXml)
MyNode.InnerText = "Lake Forest"
MessageBox.Show(MyNode.OuterXml)
MessageBox.Show(MyXMLDocument.InnerXml)
End Sub
What’s the difference?
The XML concepts in .NET are very similar to the concepts in Visual Basic 6.0 and the System.Xml object embraces the standards set forth by the W3C specifications. Additionally, Microsoft has extended this base functionality with subtle refinements over the Visual Basic 6.0 implementation. Understanding these subtle differences will help us in the transition to the new .NET platform. The gains in performance and flexibility are worth the transition effort.
The .text property is no longer supported. This property is now called .InnerText in .NET.
The XML property is no longer supported in .NET. It is now called outerXML.
To set the value of an attribute, we used to write:
oNode.setAttribute ("Value", strNode)
In .NET, this has now been changed to:
oNode.Attributes.GetNamedItem ("Value").innerText = strNode
To get the value of an attribute, we used to write:
oXML.selectSingleNode("DataSet").getAttribute("Value")
In .NET, this has now been changed to:
oXML.selectSingleNode("DataSet").Attributes.GetNamedItem("Value").innerText
Before getting the value of an attribute, it is better to check its existence by using the following code:
If Not IsNothing(oDataSetNode.Attributes.GetNamedItem("Type")) Then
To transform an XML using an XSL, we used to write the following:
Response.Write oXML.transformNode (oXSL)
In .NET, this has now been changed to:
Response.Write (oXSL.Transform(oXML.DocumentElement, Nothing).ReadOuterXml())
In order to check whether the Request object contains an XML, we used to write:
If (oXML.Load (Request)) Then
…
End If
In .NET, this code becomes:
nRequestLength = Request.TotalBytes ()
If (nRequestLength > 0) Then
btArr = Request.BinaryRead (nRequestLength)
oXML = Nothing
oXML = New XmlDataDocument()
oXML.LoadXml (System.Text.Encoding.Default.GetString (btArr))
In order to create an element, we used to do the following:
oXML.CreateNode ("ELEMENT", "InstallationsID", "")
In .NET, this has now changed to:
oXML.CreateNode (XmlNodeType.Element, "InstallationsID", "")
To transform a node to an object, we used to do the following:
oXML.transformNodeToObject (objMasterXSL, objCustomXSL)
In .NET, this has now been changed to:
objMasterXSL.Transform (oXML.DocumentElement, Nothing, oWriter)
oWriter.WriteNode (oReader, False)
objCustomXSL.Load (oReader)
To get the name of a node, we used to write:
oNode.parentNode.nodeName
In .NET, this now becomes:
oNode.parentNode.Name
To get the number of elements within a node, we used to write:
oNodes.Length
In .NET, this now becomes:
oNodes.Count
To add an attribute to a node, we used to write the following:
oNodeCol.Item (nCount).setAttribute ("BLANK", "TRUE")
In .NET, this now becomes:
Dim oAttribute As XmlAttribute
oAttribute = oMyXML.CreateAttribute ("BLANK")
oAttribute.Value = "TRUE"
oNodeCol.Item(nCount).Attributes.SetNamedItem(oAttribute)
To append a child to a node, we use the appendChild method, as shown:
oLabels.SelectSingleNode("DataSet/Labels").AppendChild
(oStdLabels.SelectSingleNode("DataSet/Labels/Screen").cloneNode(True))
In .NET, this task is now performed as follows:
oLabels.SelectSingleNode("DataSet/Labels").AppendChild
(oLabels.ImportNode(oStdLabels.SelectSingleNode("DataSet/Labels/Screen"), True))
Since $ieq$ is not supported in .NET, the following statement will give an error:
objRecordNodesList = objXMLDOMDocument.SelectNodes
("DataSet//" & strTableName & "/Record[RecordState $ieq$ 'INSERT'
Or RecordState $ieq$ 'UPDATE']")
One solution to this issue is to loop through all the record state nodes and change the case to upper case and then use the (=) operator in the above code.
To send back the response as an XML stream, we used to do the following:
oXML.Save(Response)
In .NET, this now becomes:
oXML.Save(Response.OutputStream())
To create an XML DOM object, we used to do the following:
Set oXML = CreateObject ("MSXML.DOMDocument")
In .NET, this now becomes:
oXML = New XmlDataDocument(). If we refer to a global variable, then it needs to be set to Nothing before being initialized again.
This document has been adapted from the following MSDN link: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/sysxmlvxmldom.asp
|
Responses
|
| Author: Mohit Arora 21 Jun 2004 | Member Level: Bronze Points : 0 | The article is really very good. It has clearly elaborated all the methods to work with XML and .Net. Kudos to the author for providing such a information in a single page. Thanks.
| | Author: Todd Taylor 19 Oct 2004 | Member Level: Bronze Points : 0 | I wish I would've found this article about 12 hours ago when I began my latest project! (Better late than never, I guess :) Once I found this article, it answered all my questions that I couldn't seem to find else-where, and now my project is done :)
|
|