Sunteți pe pagina 1din 15

Module 1: Week4

Walkthrough 4.0
Selecting Nodes with XPath

1. Add a new form to the project. Name the new form M1W4W40.vb.
2. Add a Label control, a TextBox control (txtXPath), a Button control (btnEvaluate), and
a ListBox control (lbNodes) to the form.
3. Switch to the code view and add the following using directive:
Imports System.Xml
4. Double-click the Button control and add the following code to handle the button's Click
event:
Private Sub btnEvaluate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Load the Books.xml file
Dim xtr As XmlTextReader = New XmlTextReader("..\..\Books.xml")
xtr.WhitespaceHandling = WhitespaceHandling.None
Dim xd As XmlDocument = New XmlDocument()
xd.Load(xtr)
' Retrieve nodes to match the expression
Dim xnl As XmlNodeList = xd.DocumentElement.SelectNodes(txtXPath.Text)
' And dump the results
lbNodes.Items.Clear()
Dim xnod As XmlNode
For Each xnod In xnl
' For elements, display the corresponding
' Text entity
If xnod.NodeType = XmlNodeType.Element Then
lbNodes.Items.Add(xnod.NodeType.ToString() + ": " +
xnod.Name + " = " + xnod.FirstChild.Value)
Else
lbNodes.Items.Add(xnod.NodeType.ToString()+ ": " + xnod.Name + " = " + xnod.Value)
End If
Next
' Clean up
xtr.Close()
End Sub
5. Insert the Main() method to launch the form. Set the form as the startup object for the
project.
6. Run the project. Enter an XPath expression in the TextBox control. Click the button to
see the nodes that the expression selects from the Books.xml file

ATS Module1(Week4) Walkthrough.doc Page 1 of 15


©2005Accenture. All Rights Reserved. Confidential
ATS Module1(Week4) Walkthrough.doc Page 2 of 15
©2005Accenture. All Rights Reserved. Confidential
Module 1: Week4
Walkthrough 4.1
Selecting Nodes with an XPathNavigator Object

1. Add a new form to the project. Name the new form M1W4W41.vb.
2. Add a Label control, a TextBox control (txtXPath), a Button control (btnEvaluate), and
a ListBox control (lbNodes) to the form.
3. Switch to the code view and add the following using directive:
Imports System.Xml.XPath
4. Double-click the Button control and add the following code to handle the button's Click
event:
Private Sub btnEvaluate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Load the Books.xml file
Dim xpd As XPathDocument = New XPathDocument("..\..\Books.xml")
' Get the associated navigator
Dim xpn As XPathNavigator = xpd.CreateNavigator()
' Retrieve nodes to match the expression
Dim xpni As XPathNodeIterator = xpn.Select(txtXPath.Text)
' And dump the results
lbNodes.Items.Clear()
While xpni.MoveNext()
lbNodes.Items.Add(xpni.Current.NodeType.ToString() + ": " + xpni.Current.Name + " = " +
xpni.Current.Value)
End While
End Sub
5. Insert the Main() method to launch the form. Set the form as the startup object for the
project.
6. Run the project. Enter an XPath expression in the TextBox control. Click the button to
see the nodes that the expression selects from the Books.xml file.

ATS Module1(Week4) Walkthrough.doc Page 3 of 15


©2005Accenture. All Rights Reserved. Confidential
Module 1: Week4
Walkthrough 4.2
Navigating with an XPathNavigator Object

1. Add a new form to the project. Name the new form M1W5W42.vb.
2. Add four Button controls (btnParent, btnPrevious, btnNext, and btnChild), and a
ListBox control (lbNodes) to the form.
3. Switch to the code view and add the following using directive:
Imports System.Xml.XPath
4. Double-click the form and add the following code to the class definition:
Dim xpd As XPathDocument
Dim xpn As XPathNavigator

Private Sub Exercise_9_Load(ByVal sender As Object, ByVal e As System.EventArgs)


' Load the Books.xml file
xpd = New XPathDocument("..\..\Books.xml")
' Get the associated navigator
xpn = xpd.CreateNavigator()
xpn.MoveToRoot()
ListNode()
End Sub

Private Sub ListNode()


' Dump the current node to the listbox
lbNodes.Items.Add(xpn.NodeType.ToString() + ": " + xpn.Name + " = " + xpn.Value)
End Sub
5. Double-click the Button controls and add the following code to their Click event
handlers:
Private Sub btnParent_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Move to the parent of the current node
If xpn.MoveToParent() Then
ListNode()
Else
lbNodes.Items.Add("No parent node")
End If
End Sub

Private Sub btnChild_Click(ByVal sender As Object, ByVal e As System.EventArgs)


' Move to the first child of the current node
If xpn.MoveToFirstChild() Then
ListNode()
Else
lbNodes.Items.Add("No child node")
End If
End Sub

Private Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs)


' Move to the previous sibling
' of the current node
If xpn.MoveToPrevious() Then
ListNode()
Else
lbNodes.Items.Add("No previous node")
End If
End Sub

Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs)


' Move to the next sibling of the current node
If xpn.MoveToNext() Then
ListNode()
Else
lbNodes.Items.Add("No next node")
End If

ATS Module1(Week4) Walkthrough.doc Page 4 of 15


©2005Accenture. All Rights Reserved. Confidential
End Sub

6. Insert the Main() method to launch the form. Set the form as the startup object for the
project.
7. Run the project. Experiment with the buttons. You'll find that you can move around in
the DOM

ATS Module1(Week4) Walkthrough.doc Page 5 of 15


©2005Accenture. All Rights Reserved. Confidential
Module 1: Week4
Walkthrough 4.3
Extracting an XML Schema

1. Add a new form to the project. Name the new form M1W4W43.vb.
2. Add a Button control (btnGetSchema) and a TextBox control (txtSchema) to the
form. Set the MultiLine property of the TextBox to true and set its ScrollBars
property to Vertical.
3. Switch to the code view and add the following using directives:
Imports System.Xml
Imports System.Data
Imports System.IO
4. Double-click the Button controls and add code to process an XML document when you
click the Button control:
Private Sub btnGetSchema_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Load the XML file with inline schema info
Dim xtr As XmlTextReader = New XmlTextReader("..\..\Products.xml")
' Read the schema (only) into a DataSet
Dim ds As DataSet = New DataSet()
ds.ReadXmlSchema(xtr)
' Write the schema out as a separate stream
Dim sw As StringWriter = New StringWriter()
ds.WriteXmlSchema(sw)
txtSchema.Text = sw.ToString()
' Clean up
xtr.Close()
End Sub
5. Add a new XML file to the project. Name the new file Products.xml. Add this XML to
the new file:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:od="urn:schemas-microsoft-com:officedata">
<xsd:schema>
<xsd:element name="dataroot">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element ref="Products"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
<xsd:element name="Products">
<xsd:annotation>
<xsd:appinfo/>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="ProductID"
od:jetType="autonumber"
od:sqlSType="int" od:autoUnique="yes"
od:nonNullable="yes">
<xsd:simpleType>
<xsd:restriction base="xsd:integer"/>
</xsd:simpleType>
</xsd:element>
<xsd:element name="ProductName" minOccurs="0"
od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="40"/>
</xsd:restriction>
</xsd:simpleType>

ATS Module1(Week4) Walkthrough.doc Page 6 of 15


©2005Accenture. All Rights Reserved. Confidential
</xsd:element>
<xsd:element name="SupplierID" minOccurs="0"
od:jetType="longinteger" od:sqlSType="int">
<xsd:simpleType>
<xsd:restriction base="xsd:integer"/>
</xsd:simpleType>
</xsd:element>
<xsd:element name="CategoryID" minOccurs="0"
od:jetType="longinteger" od:sqlSType="int">
<xsd:simpleType>
<xsd:restriction base="xsd:integer"/>
</xsd:simpleType>
</xsd:element>
<xsd:element name="QuantityPerUnit" minOccurs="0"
od:jetType="text" od:sqlSType="nvarchar">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="UnitPrice" minOccurs="0"
od:jetType="currency"
od:sqlSType="money" type="xsd:double"/>
<xsd:element name="UnitsInStock" minOccurs="0"
od:jetType="integer"
od:sqlSType="smallint" type="xsd:short"/>
<xsd:element name="UnitsOnOrder" minOccurs="0"
od:jetType="integer"
od:sqlSType="smallint" type="xsd:short"/>
<xsd:element name="ReorderLevel" minOccurs="0"
od:jetType="integer"
od:sqlSType="smallint" type="xsd:short"/>
<xsd:element name="Discontinued"
od:jetType="yesno"
od:sqlSType="bit" od:nonNullable="yes"
type="xsd:byte"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<dataroot xmlns:xsi=
"http://www.w3.org/2000/10/XMLSchema-instance">
<Products>
<ProductID>1</ProductID>
<ProductName>Chai</ProductName>
<SupplierID>1</SupplierID>
<CategoryID>1</CategoryID>
<QuantityPerUnit>10 boxes x 20 bags
</QuantityPerUnit>
<UnitPrice>18</UnitPrice>
<UnitsInStock>39</UnitsInStock>
<UnitsOnOrder>0</UnitsOnOrder>
<ReorderLevel>10</ReorderLevel>
<Discontinued>0</Discontinued>
</Products>
<Products>
<ProductID>2</ProductID>
<ProductName>Chang</ProductName>
<SupplierID>1</SupplierID>
<CategoryID>1</CategoryID>
<QuantityPerUnit>24 - 12 oz bottles
</QuantityPerUnit>
<UnitPrice>19</UnitPrice>
<UnitsInStock>17</UnitsInStock>
<UnitsOnOrder>40</UnitsOnOrder>
<ReorderLevel>25</ReorderLevel>
ATS Module1(Week4) Walkthrough.doc Page 7 of 15
©2005Accenture. All Rights Reserved. Confidential
<Discontinued>0</Discontinued>
</Products>
<Products>
<ProductID>3</ProductID>
<ProductName>Aniseed Syrup</ProductName>
<SupplierID>1</SupplierID>
<CategoryID>2</CategoryID>
<QuantityPerUnit>12 - 550 ml bottles
</QuantityPerUnit>
<UnitPrice>10</UnitPrice>
<UnitsInStock>13</UnitsInStock>
<UnitsOnOrder>70</UnitsOnOrder>
<ReorderLevel>25</ReorderLevel>
<Discontinued>0</Discontinued>
</Products>
<Products>
<ProductID>4</ProductID>
<ProductName>
<![CDATA[Chef Anton's Cajun Seasoning]]>
</ProductName>
<SupplierID>2</SupplierID>
<CategoryID>2</CategoryID>
<QuantityPerUnit>48 - 6 oz jars</QuantityPerUnit>
<UnitPrice>22</UnitPrice>
<UnitsInStock>53</UnitsInStock>
<UnitsOnOrder>0</UnitsOnOrder>
<ReorderLevel>0</ReorderLevel>
<Discontinued>0</Discontinued>
</Products>
<Products>
<ProductID>5</ProductID>
<ProductName><![CDATA[Chef Anton's Gumbo Mix]]>
</ProductName>
<SupplierID>2</SupplierID>
<CategoryID>2</CategoryID>
<QuantityPerUnit>36 boxes</QuantityPerUnit>
<UnitPrice>21.35</UnitPrice>
<UnitsInStock>0</UnitsInStock>
<UnitsOnOrder>0</UnitsOnOrder>
<ReorderLevel>0</ReorderLevel>
<Discontinued>1</Discontinued>
</Products>
<Products>
<ProductID>6</ProductID>
<ProductName>
<![CDATA[Grandma's Boysenberry Spread]]>
</ProductName>
<SupplierID>3</SupplierID>
<CategoryID>2</CategoryID>
<QuantityPerUnit>12 - 8 oz jars</QuantityPerUnit>
<UnitPrice>25</UnitPrice>
<UnitsInStock>120</UnitsInStock>
<UnitsOnOrder>0</UnitsOnOrder>
<ReorderLevel>25</ReorderLevel>
<Discontinued>0</Discontinued>
</Products>
</dataroot>
</root>
6. Insert the Main() method to launch the form. Set the form as the startup object for the
project.
7. Run the project. Click the button to load the XML file and extract the inline schema
information to the text box

ATS Module1(Week4) Walkthrough.doc Page 8 of 15


©2005Accenture. All Rights Reserved. Confidential
ATS Module1(Week4) Walkthrough.doc Page 9 of 15
©2005Accenture. All Rights Reserved. Confidential
Module 1: Week4
Walkthrough 4.4
Inferring an XML Schema

1. Add a new form to the project. Name the new form M1W4W44.vb.
2. Add a Button control (btnInferSchema) and a TextBox control (txtSchema) to the
form. Set the MultiLine property of the TextBox to true and set its ScrollBars
property to Vertical.
3. Switch to the code view and add the following using directives:
Imports System.Xml
Imports System.Data
Imports System.IO
4. Double-click the Button controls and add code to process an XML document when you
click the Button control:
Private Sub btnInferSchema_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Load an XML file with no schema information
Dim xtr As XmlTextReader = New XmlTextReader("..\..\Books.xml")
' Read the schema (only) into a DataSet
Dim ds As DataSet = New DataSet()
Dim ns() As String = {}

ds.InferXmlSchema(xtr, ns)
' Write the schema out as a separate stream
Dim sw As StringWriter = New StringWriter()
ds.WriteXmlSchema(sw)
txtSchema.Text = sw.ToString()
' Clean up
xtr.Close()
End Sub
5. Insert the Main() method to launch the form. Set the form as the startup object for the
project.
6. Run the project. Click the button to load the XML file and infer the schema information
to the text box

ATS Module1(Week4) Walkthrough.doc Page 10 of 15


©2005Accenture. All Rights Reserved. Confidential
ATS Module1(Week4) Walkthrough.doc Page 11 of 15
©2005Accenture. All Rights Reserved. Confidential
Module 1: Week4
Walkthrough 4.5
Validating an XML Document Against an Inline Schema

1. Add a new form to the project. Name the new form M1W4W45.vb.
2. Add a Button control (btnValidate) and a TextBox control (txtErrors) to the form. Set
the MultiLine property of the TextBox to true and set its ScrollBars property to
Vertical.
3. Switch to the code view and add the following using directives:
Imports System.Xml
Imports System.Xml.Schema
4. Double-click the Button controls and add code to validate an XML document when you
click the Button control:
Private Sub btnValidate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Load a document with an inline schema
Dim xtr As XmlTextReader = New XmlTextReader("..\..\Products.xml")
' Prepare to validate it
Dim xvr As XmlValidatingReader = New XmlValidatingReader(xtr)
xvr.ValidationType = ValidationType.Schema
' Tell the validator what to do with errors
xvr.ValidationEventHandler += New ValidationEventHandler(ValidationHandler)
' Load the document, thus validating
Dim xd As XmlDocument = New XmlDocument()
xd.Load(xvr)
' Clean up
xvr.Close()
End Sub

Public Sub ValidationHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)


' Dump any validation errors to the UI
txtErrors.AppendText(e.Message + "\n")
End Sub
5. Insert the Main() method to launch the form. Set the form as the startup object for the
project.
6. Run the project. Click the button to load and simultaneously validate the XML file

ATS Module1(Week4) Walkthrough.doc Page 12 of 15


©2005Accenture. All Rights Reserved. Confidential
7. Stop the project. Open the Products.xml file and make a change. For example, change
the name of a child element from SupplierID to SupplierIdentifier
8. Run the project. Click the button to load and simultaneously validate the XML file.
You'll see additional validation errors

ATS Module1(Week4) Walkthrough.doc Page 13 of 15


©2005Accenture. All Rights Reserved. Confidential
Module 1: Week4
Walkthrough 4.6
Validating an XML Document Against a DTD

1. Add a new form to the project. Name the new form M1W4W46.vb.
2. Add a Button control (btnValidate) and a TextBox control (txtErrors) to the form.
Set the MultiLine property of the TextBox to true and set its ScrollBars property to
Vertical.
3. Switch to the code view and add the following using directives:
using System.Xml;
using System.Xml.Schema;
4. Double-click the Button controls and add code to validate an XML document when you
click the Button control:
private void btnValidate_Click(object sender, System.EventArgs e)
{
// Load a document with a DTD
XmlTextReader xtr = new XmlTextReader(@"..\..\Books3.xml");
// Prepare to validate it
XmlValidatingReader xvr =
new XmlValidatingReader(xtr);
xvr.ValidationType = ValidationType.DTD;
// Tell the validator what to do with errors
xvr.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);
// Load the document, thus validating
XmlDocument xd = new XmlDocument();
xd.Load(xvr);
// Clean up
xvr.Close();
}

public void ValidationHandler(object sender, ValidationEventArgs e)


{
// Dump any validation errors to the UI
txtErrors.AppendText(e.Message + "\n");
}
5. Add a new XML file to the project. Name the new file Books3.xml. Enter the following
text for Books3.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Books SYSTEM "books.dtd">
<Books>
<Book Pages="1088">
<Author>Delaney, Kalen</Author>
<Title>Inside Microsoft SQL Server 2000</Title>
<Publisher>Microsoft Press</Publisher>
</Book>
<Book Pages="997">
<Author>Burton, Kevin</Author>
<Title>.NET Common Language Runtime</Title>
<Publisher>Sams</Publisher>
</Book>
<Book Pages="392">
<Author>Cooper, James W.</Author>
<Title>C# Design Patterns</Title>
<Publisher>Addison Wesley</Publisher>
</Book>
</Books>
ATS Module1(Week4) Walkthrough.doc Page 14 of 15
©2005Accenture. All Rights Reserved. Confidential
6. Add a new text file to the project. Name the new schema file Books.dtd. Enter the
following text for Books.dtd:
<!ELEMENT Books (Book)* >
<!ELEMENT Book (Author, Title, Publisher) >
<!ATTLIST Book Pages CDATA #REQUIRED>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Publisher (#PCDATA)>
7. Insert the Main() method to launch the form. Set the form as the startup object for the
project.
8. Run the project. Click the button to load and simultaneously validate the XML file.
Because the file exactly matches the schema, you won't see any errors.
9. Stop the project. Open the Books3.xml file and make a change. For example, change
the name of a child element from Author to Writer.
10. Run the project. Click the button to load and simultaneously validate the XML file.
You'll see validation errors

ATS Module1(Week4) Walkthrough.doc Page 15 of 15


©2005Accenture. All Rights Reserved. Confidential

S-ar putea să vă placă și