Tài liệu Work with the XML Document Object Model - Pdf 98

12.3 Work with the XML Document Object Model
I want to have more control over the XML document as I create it. I heard that I can do
this with XML DOM. How do I work with the XML Document Object Model?
Technique
In How-Tos 12.1 and 12.2, you saw two ways to independently read and write XML
documents. However, what if you want to do both at the same time? To have this kind of
flexibility, you need to use the XML Document Object Model, also known as DOM.
The DOM class is an XML document that is represented in memory. It allows you not
only to programmatically read and write out XML documents, but also to modify those
documents in memory.
Within the DOM, the XMLNode object is the base object in the DOM Tree,
XMLDocument class that extends it. XMLDocument has methods that allow you to
perform operations on the document as a whole. It also lets the developer work with the
nodes in the entire XML document.
Both XMLNode and XMLDocument have performance and usability enhancements over
prior versions.
The properties and methods of XMLNode and XMLDocument that will be used for this
How-To are listed in Table 12.6.
Table 12.6. DOM Properties and Methods Used in This How-To
Class Name Property/Method Purpose/Description
XMLDocument LoadXML Loads an XML document into the XMLDocument
object. In this case, it is a means to create the stub
for the XML document that will be created from
the dataset.
XMLDocument DocumentElement Serves as the root element for the document. It is,
in fact, of the type XMLElement.
XMLDocument CreateNode Creates an XMLNode object.
XMLElement AppendChild Appends the node created to the element specified
as a child.
XMLDocument CreateComment Adds a comment to the XML document.
XMLNode AppendChild Appends a node to another node as a child.

entry already exists, it is reassigned to the module variable mdtData. Last, the data
table is bound to the DataGrid object by calling the BindTheGrid routine, which is
described in the next step.
Listing 12.6 wfrmHowTo12_3.aspx.vb: Creating a DataTable Object from
Scratch
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here
If (Session("MyDataTable") Is Nothing) Then

Dim dcFirstName As New DataColumn()

dcFirstName.ColumnName = "FirstName"
dcFirstName.Caption = "First Name"

mdtData.Columns.Add(dcFirstName)

Dim dcLastName As New DataColumn()

dcLastName.ColumnName = "LastName"
dcLastName.Caption = "Last Name"

mdtData.Columns.Add(dcLastName)

Session("MyDataTable") = mdtData

Else
mdtData = CType(Session("MyDataTable"), DataTable)
End If

drNew.Item("FirstName") = Me.txtFirstName.Text

mdtData.Rows.Add(drNew)

Me.txtLastName.Text = ""
Me.txtFirstName.Text = ""

BindTheGrid()

End Sub
7. Add the code in Listing 12.9 to the Click event of the btnCreateXMLFile button.
After getting the number of rows in mdtData, an XML document is started using
the LoadXML method. Next, the root element is retrieved so that nodes can then
be "hung" from it, using the CreateNode and AppendChild methods. A comment
is then added using the CreateComment method of xdMyData. Then, for each of
the rows in the mdtData, nodName is created using CreateNode and AppendChild
methods, and node on nodName is added for the LastName and FirstName. Again,
these nodes are added using the CreateNode and AppendChild methods. Last, the
Save method is used to save the XML document.
Listing 12.9 wfrmHowTo12_3.aspx.vb: Creating the XML Document
Private Sub btnCreateXMLFile_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCreateXMLFile.Click

Dim xdMyData As New System.Xml.XmlDocument()
Dim xeRoot As System.Xml.XmlElement

Dim intCurrRow As Integer
Dim intNumRows As Integer

intNumRows = mdtData.Rows.Count - 1

nodName.AppendChild(nodFirstName)

Dim nodLastName As System.Xml.XmlNode = _
.CreateNode(System.Xml.XmlNodeType.Element, "LastName",
"")
nodLastName.InnerText =
mdtData.Rows(intCurrRow).Item("LastName")

nodName.AppendChild(nodLastName)

Next

' Write the XML to file and close the writer
.Save("c:\Test.xml")

End With

End Sub
Figure 12.3. This XML document was created using XMLTextWriter.

Comments
As you can see, working with the DOM takes a bit more time and work. However, when
you really need to massage the data, this is the way to go!


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status