perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
home » Free Library » Wrox Press » Beginning Visual Basic .NET Databases Sunday, 15 July 2007
Reproduced with kind permission of Wrox Press
Chapter 12: ADO.NET and XML
ISBN: 1861005555
Author(s): Matthew Reynolds, Denise Gosnell, Bill Forgey
What does this book cover?
  • The basic principles of relational database design
  • Microsoft SQL Server Desktop Engine
  • Querying the database with the T-SQL language
  • Visual Studio .NET and the Server Explorer
  • ADO.NET and the DataSet
  • Data binding, updating the database, and conflict resolution
  • XML's role in ADO.NET
  • Accessing data with ASP.NET and Web Services
Page 6 of 7

Go to page: 1   2   3   4   5   6   7   

12

ADO.NET and XML

Changing the DataSet Changes the XML

So we've seen changes work one way. Let's see if we can make changes to the DataSet update the XML document.

To do this, we'll need to create a way of displaying the contents of the XmlDataDocument object from within our own code. We'll create a separate form containing a single ListBox control and add a method that will let us update the view whenever we suspect that the data in the DataSet has changed.

Try It Out - Changing DataSet Data
  1. If the project is running, close it.
     
  2. Using Solution Explorer, right-click on the Order Export project and select Add | Add Windows Form. Call the new form XmlDocumentView.
     
  3. Paint on a new ListBox control. Set its Name property to lstNodes, IntegralHeight property to False and Anchor property to Top, Bottom, Left, Right.

  1. Open the code editor for the form. Add this namespace import declaration:
Imports System.Xml

Public Class XmlDocumentView

 Inherits System.Windows.Forms.Form

  1. Next, add a new member to the class:
Public Class XmlDocumentView
 Inherits System.Windows.Forms.Form
 
 ' Members...
 Private _document As XmlDataDocument
  1. Next, add this property:

' Document - document property...
 Public Property Document() As XmlDataDocument
   Get
     Return _document
   End Get
   Set(ByVal Value As XmlDataDocument)
     UpdateView()
   End Set
 End Property

  1. Finally, add these two methods:

' UpdateView - update the view...
 Public Sub UpdateView()

   ' Clear the list...
   lstNodes.Items.Clear()

   ' Do we have a document?
   If Not Document Is Nothing Then

     ' Start adding items...
     DoUpdateView(Document.FirstChild, 0)

   End If

 End Sub

 ' DoUpdateView - go through adding nodes...
 Protected Sub DoUpdateView(ByVal node As XmlNode, ByVal level As Integer)

   ' Go through the nodes...
   Do While Not node Is Nothing

     ' Create a new string...
     Dim nodeString As String = ""
     Dim n As Integer
     For n = 0 To level - 1
         nodeString &= " "

     Next
     nodeString &= node.NodeType.ToString() & ":"
     If node.Value = "" Then
       nodeString &= node.Name
     Else
       nodeString &= node.Value
     End If

     ' Add it...
     lstNodes.Items.Add(nodeString)

     ' Do the children...
     DoUpdateView(node.FirstChild, level + 1)

     ' Next...
     node = node.NextSibling

   Loop

 End Sub

  1. That's all we need to do in order to create a form that lets us view the current contents of an XmlDataDocument object.

  2. Open the code editor for Form1. Add this member:

Public Class Form1
  Inherits System.Windows.Forms.Form

  ' Members...
  Private _dataset As DataSet
  Private _document As XmlDataDocument

 Private _documentView As XmlDocumentView
  1. Flip over to the Designer for Form1 and double-click on the form background. This will create a new Load event handler. Add this code:

 Private Sub Form1_Load(ByVal sender As System.Object, _
                   ByVal e As System.EventArgs) Handles MyBase.Load

   ' Show the view...
   
_documentView = New XmlDocumentView()
    _documentView.Show()
 End Sub
  1. Find the DataSet property. Add this code:

...
     ' Create the document...
     _document = New XmlDataDocument(_dataset)

     ' Update the view...
     _documentView.Document = _document

   End Set

 End Property

  1. Using the drop-down list in the top left-hand corner of the editor window, select dgdOrders. From the right-hand list select CurrentCellChanged.
     
  2. When the new event handler has been created, add this code:

Private Sub dgdOrders_CurrentCellChanged(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles dgOrders.CurrentCellChanged

    _documentView.UpdateView()
 End Sub
  1. Again, using the drop-down list, select (Overrides) from the left-hand list and select OnClosed from the right-hand list. Add this code:
Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
    _documentView.Close()
    _documentView = Nothing
End Sub
  1. Run the project. The new view window will appear. Click Load or Connect to load up the document and you'll notice that the list in the view window becomes populated with data.

  1. You can see here that I've selected the value contained within the CustomerID element for order 11077. Using the DataGrid, change this value to DIZZY.

When you click outside of the cell or press Return, the changes will be made to the DataSet and the CurrentCellChanged event will be fired. You'll also notice that the XML document has also been updated.

How It Works

So we've proved now that not only do changes to the XmlDataDocument object affect the DataSet, but also that the opposite is true.

It's worth looking at how we built up the view, as it's another example of how we can use recursion to walk through the nodes that make up the document. In fact, because we can see the results, it may make the process clearer if it's still a little foggy.

Again, when DoUpdateView is called, we pass in the node that represents the starting position. At first, this will be the start tag for the top-level element.

 ' DoUpdateView - go through adding nodes...
 Protected Sub DoUpdateView(ByVal node As XmlNode, ByVal level As Integer)

   ' Go through the nodes...
   Do While Not node Is Nothing

For each one, we want to make up a string and add it to the ListBox. Depending on the level, we want to indent the string so on the first level there is no indentation, on the second level there's some indentation, on the third there's a little more, and so on. The level that we're working at will be passed in through the level parameter.

     ' Create a new string...
     Dim nodeString As String = ""

     Dim n As Integer
     For n = 0 To level - 1
         nodeString &= " "

     Next

Once we've added an indent to a string, we can render the type that the node is. We then tack on either the name of the element or, if we have one, the current value.

     nodeString &= node.NodeType.ToString() & ":"
     If node.Value = "" Then
       nodeString &= node.Name
     Else
       nodeString &= node.Value
     End If

Then we can add the string to the ListBox:

     ' Add it...
     lstNodes.Items.Add(nodeString)

As before, as soon as we've done one element we need to call into the function again to do the children. We pass an incremented version of level into the function, and this lets us adjust the indentation.

     ' Do the children...
     DoUpdateView(node.FirstChild, level + 1)

After we've walked through the children, we can move onto the next sibling.

     ' Next...
     node = node.NextSibling

   Loop

 End Sub

OK, so that's how the view is put together, and you can see that it follows the structure of the document as we see it displayed in Internet Explorer. (Using this method the end tags aren't displayed, but this is no big deal!) But, have we really proven that the objects are being synchronized, or are we showing that some funny business is going on?

If we look at the DataSet property, we can see that it's at that point that we set the Document property on our XmlDocumentView object.

 ' DataSet property...
 Public Property DataSet() As DataSet
   Get
     Return _dataset
   End Get
   Set(ByVal Value As DataSet)

     ' Save it...
     _dataset = Value

     ' Bind...
     datagridOrders.DataSource = _dataset
     datagridOrders.DataMember = _dataset.Tables(0).TableName

     ' Create the document...
     _document = New XmlDataDocument(_dataset)

     ' Update the view...
     _documentView.Document = _document

   End Set

 End Property

This is the only time this happens, and the only time that the DataSet property is set is after we've pressed the Connect or Load buttons.

When the current cell is changed on the DataGrid, we run this code:

 Private Sub datagridOrders_CurrentCellChanged(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles datagridOrders.CurrentCellChanged
     _documentView.UpdateView()

 End Sub

We know that this code just goes through the document that's stored in the DataSet public member on  XmlDocumentView. As this isn't changing, the only reasonable conclusion that we can come to is that the DataSet is indeed updating the XmlDataDocument.


Page 6 of 7

Go to page: 1   2   3   4   5   6   7   

12

ADO.NET and XML

  Contact Us |  | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy