Documente Academic
Documente Profesional
Documente Cultură
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
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.
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
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
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>
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
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
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();
}