111
uthor: Bill Buchanan uthor: Bill Buchanan .NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Embed Size (px)

Citation preview

Page 1: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

.NET XML Web Services.NET XML Web Services

Alistair Lawson, SoC

Bill Buchanan, SoC

Page 2: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Date Title Kalani

Monday 4 June 11-12 Unit 0: Introduction to .NET -

Monday 4 June 2-4pm Unit 0: Introduction to .NET -

Tuesday 5 June 10-12 Unit 1: Creating/Manipulating Datasets Unit 1

Tuesday 5 June 2-4pm Unit 1: Creating/Manipulating Datasets Unit 1

Monday 11 June Unit 2: Manipulating XML Data Unit 2

Tuesday 12 June Unit 2: Manipulating XML Data Unit 2

Monday 18 June Unit 3: .NET Remoting Unit 3

Tuesday 19 June Unit 3: .NET Remoting Unit 3

Monday 25 June Unit 4: Web Services Unit 4

Tuesday 26 June Unit 4: Web Services Unit 5

Page 3: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Accessing and Manipulating XMLAccessing and Manipulating XML

Alistair Lawson, SoC

Bill Buchanan, SoC

Page 4: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Consuming and Manipulating DataConsuming and Manipulating Data

Access and manipulate data from a Microsoft SQL Server™ database by creating and using ad hoc queries and stored procedures.

Create and manipulate DataSets. Manipulate a DataSet schema. Manipulate DataSet relationships. Create a strongly typed DataSet.

Access and manipulate XML data. Access an XML file by using the Document Object Model (DOM) and an XmlReader. Transform DataSet data into XML data. Use XPath to query XML data. Generate and use an XSD schema. Write a SQL statement that retrieves XML data from a SQL Server database. Update a SQL Server database by using XML. Validate an XML document.

Page 5: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

Introduction

Bill Buchanan

Page 6: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Objectives

• Read XML from the disk and to fill up a dataset.• Create an XML and XML schema files from a

stored dataset.• Extract SQL Server data in an XML format, and to

be able to update the SQL Server database with XML updates.

Page 7: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

MS AccessMS Access

My SQLMy SQL

OracleOracle

CSVCSV

XMLXML

XML SchemaXML Schema

Program

Binary filesBinary files

LinuxLinux

WindowsWindows

OS2OS2

UNIXUNIX

Page 8: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

DataSet

DataView DataRow

SqlCommand

SqlAdapter

SqlConnection

DataGrid ListBox CheckBox

BinaryReader

BinaryWriter

FileStream

StreamReader

StreamWriter

FileStream

XmlNode

XmlDocument

XmlTextReader

DataModel

DataStore

DatabaseBinary file Text file XML file

UserInterface

SqlReader

Page 9: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

• The DataSet object thus provides a generalized abstraction of the data source, which gets rid of any differences in the methods used to store the data.

• The DataSet loaded into memory from the data source, and can thus be accessed faster than reading the data from its source.

• The DataSet is stored in a relational way which allows one-to-one and one-to-many relationships.

SqlCommand

SqlAdapter

SqlConnection

BinaryReader

BinaryWriter

FileStream

StreamReader

StreamWriter

FileStream

XmlNode

XmlDocument

XmlTextReaderDataStore

DatabaseBinary file Text file XML file

SqlReader

Page 10: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

• The DataSet contains a schema, which defines the form of the stored data, such as its structure and its data types. This is defined as metadata. .NET provides an environment for the creation, editing and updating for this schema. An important element is that a strongly typed DataSet allows the data to be checked at any early stage, and should result in fewer errors.

SqlCommand

SqlAdapter

SqlConnection

BinaryReader

BinaryWriter

FileStream

StreamReader

StreamWriter

FileStream

XmlNode

XmlDocument

XmlTextReaderDataStore

DatabaseBinary file Text file XML file

SqlReader

Page 11: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

XML

Bill Buchanan

Page 12: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

WWW languages of the past, especially HTML, have been fixed in their tags. A new format named XML (eXtensible Markup Language) can be used to create new tags, and provide a common platform for transferring information between different systems and packages.

The first line of an XML file typically contains an optional xml processing instruction (known as the XML declaration). This can contain pseudo-attributes that indicate the XML language version, the character set, and whether it can be used as a standalone entity.

An example is the XML declaration that begins every valid XML file:

<?xml version="1.0" standalone="yes" ?>

Page 13: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

The XML document conforms to the XML recommendations, and has a logical structure that is composed of declarations, elements, comments, character references, and processing instructions.

It also has a physical structure which is composed of entities, starting with the root, or document entity.

Page 14: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

The XML object model defines a standard way in which the elements of the XML structured tree are defined. It is fully object-oriented and uses properties, methods, and the actual content (data) contained in an object. This model controls how users interpret the trees, and exposes all tree elements as objects, which can be accessed without any return trips to the server. The XML object model uses the W3C standard know as Document Object Model.

Page 15: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

XML-Data Reduced (XDR) is one of the first languages defined which uses a schema (that is one that is defined in an XML form. It defines the:

Form of elements that are child elements of others.Sequence in which the child elements can appear.Number of child elements.

It also defines whether an element is empty or can include text. XDR is now well established and uses XML as its basic language. A new standard known as XSD (XML Schema Definition) has been standardized by the W3C XML Schema Working Group.

Page 16: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Root

Children

Page 17: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

RootRoot

ChildChild ChildChild ChildChild

Documentroot

First Child

Next Sibling

Page 18: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

RootRoot

RootRoot ChildChild ChildChild

Documentroot

First Child

Next SiblingChildChildChildChildChildChild

Page 19: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

RootRoot

ChildChild RootRoot ChildChild

Documentroot

ChildChildChildChildChildChild

Page 20: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

XML Elements

Bill Buchanan

Page 21: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

• An XML document can only have one document element. In XML a document element is a single element that contains all the content that is to be considered as past of the document itself. The document root is the first element that appears in the XML document.

• All XML elements must have end tags. In HTML, some tags do not require an end tag, such as <P>, whereas XML requires that every tag has an end tag.

• XML elements cannot overlap. The XML tags must be properly structured so that they do not overlap. For example the following is not allow:

<tag1>Blah blah <tag2>Blah</tag1> Blah</tag2>

In this case the XML parser will stop after </tag1> as the parser expects to find the </tag1> next.

Page 22: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

• Attribute values must contain quotes, whether or not they contain spaces. These quotes can either be a single invert comma, or a double one.

• <, >, or & cannot be used within the text the document. For these characters the entities of &lt;, &gt; and &amp; can be used.

For example: If we wanted the contents of a tag to be My <Web> then the following would be used:

<MyTag>My &lt; Web&gt;</MyTag>

Page 23: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

AttributesAn attribute is an XML structural construct. It has a name-value pair, separated by an equals sign, and is included inside a tagged element that modifies the features of an element. Thus, all attributes are string values. The following shows an example of an attribute were id_value is set to “disk001”.

<?xml version="1.0"?><software> <disk id_value="disk001"> <author>Smith, Fred</author> <title>XML Developer's Guide</title> <function>Disk tools</function> <price>100</price> <version>1.02</version> <description>Windows XP version </description> </disk></software>

Page 24: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Entity

An entity is an XML structural construct. It can be a file, a database record, or any other item that contains data. Its main aim is to hold content, and not structure, rules, or grammar. In the XML document, each entity is has a unique name and contains its own content.

Page 25: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

XML Format

Bill Buchanan

Page 26: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Prolog. This part is at the start of the document (or root element). It contains information on the document as a whole. Typical references are to the character encoding, document structure, and style sheets. An example is:

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="my_style.xsl"?><!DOCTYPE catalog SYSTEM "mycatalog.dtd"><!--This file was last updated 2002-12-10-->

The prolog can include processing instructions, such as the xml-stylesheet processing instruction (as shown in the about example). The <!-- …. --> statement contains a comment.

Page 27: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Document elements. The form the backbone of the XML documents, and creates the required structure. Within it there the elements identify named sections of information, and use markup tags that identify the name, start, and end of the element.

All elements have names, and are case-sensitive and must start with a letter or an underscore.

Tags establish boundaries around content. The start tags has the following format:

<elementName attrib1="attrib1Value" attrib2="attrib2Value"...>

If an element name does not have any attributes then its format is:

<elementName >

and the end tag has the following format (which cannot have any attributes):

</elementName>

Page 28: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Elements are contained between a start and an end tag. For example:

<pet> <type>Cat</type> <name>Honey</name></pet>

where the pet element contains two other elements:

<type> and <name>. The <type> tags contains the text ‘Cat’, and the <name> element contains ‘Honey’. An empty tag can be used when there is no textual content. An empty tag contains a slash (/) before the closing character, such as:

<help/>which is the same as:

<help></help>

Page 29: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Family and tree metaphors are used to describe the relationship between elements. All XML documents must contain a root element (which is also know as a document). In the following XML document, <pet> is the root document:

<pet> <type>Cat</type> <name>Honey</name> </pet>

The following cannot be an XML document, as it has no root document:

<type>Cat</type><name>Honey</name>

With a tree structure, the leaves refer to elements that do not contain any other elements (such as in a real tree, where leaves are ends of branches. These leaf elements generally contain simple text or no textural information.

Page 30: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

The terms used in a family metaphors are such as parent, child, ancestor, descendant, and sibling. These are used to describe relationship between elements relative to each other (and not to the entire document). The following abstract sample document illustrates the relationships between elements.

<tag1>

<tag2>

<tag3>

<tag4/>

</tag3>

</tag2>

</tag1>

Page 31: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

.NET XML Toolkit

Bill Buchanan

Page 32: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

The main namespace used with XML is System.Xml, and gives access to objects which can create, edit and manipulate XML documents. As seen in the previous unit, the main objects that are created in XML are:

• XmlDocument. This is used when read and write access is required for an XML document.

• XmlNode. This is used when access is required to the individual nodes in the XML tree.

• XmlTextReader. This is used to just read the XML document, and there is no need for access to XPath, or there is no requirement for XSD validation.

• XPath/ XPathNavigator. This is used for read access for a search on an XML document.

Page 33: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

The XmlReader and XmlTextReader classes give access to a read-only function of an XML file. XmlReader is a purely abstract class, whereas the XmlTextReader class implements XmlReader, and gives access to an XML file as a text stream. Its main methods are:

• XmlTextReader.Close() Closes the XML file• XmlTextReader.GetAttribute() Gets the value of an attribute• XmlTextReader.Read() Read the next node• XmlTextReader.Skip() Skips all the children of the current

node

and its main properties are:

• XmlTextReader.Depth Defines the depth of the XML node• XmlTestReader.EOF Defines if the end-of-file marker

XmlTextReader.Name Get the name of the node• XmlTextReader.NodeType Get the type of the node• XmlTextReader.HasValue Determine if the node has a value

property

Page 34: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Code Example 2.1 gives an example of the XmlTextReader implementation, and Sample Run 2.1 shows a test run for the following file:

<?xml version="1.0" encoding="utf-8" ?> <instruments>

<instrum><name>DVM</name><units>Volts</units>

</instrum><instrum>

<name>Watt Meter</name><units>Watts</units>

</instrum></instruments>

Page 35: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

StringBuilder sbNode = new StringBuilder();XmlTextReader xtr = new XmlTextReader(@"..\..\instrum.xml");while (xtr.Read()){

sbNode.Length=0;for (int i=1; i<=xtr.Depth; i++){sbNode.Append(" ");

}sbNode.Append("Name: " + xtr.Name + " ");sbNode.Append("Node Type: " + xtr.NodeType.ToString());if (xtr.HasValue){sbNode.Append("Value: " + xtr.Value);

}lbNodes.Items.Add(sbNode.ToString());

}xtr.Close();

}

Page 36: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Name: xml Node Type: XmlDeclarationValue: version="1.0" encoding="utf-8"Name: Node Type: Whitespace Value: Name: instruments Node Type: Element Name: Node Type: Whitespace Value: Name: instrum Node Type: Element Name: Node Type: Whitespace Value: Name: name Node Type: Element Name: Node Type: TextValue: DVM Name: name Node Type: EndElement Name: Node Type: Whitespace Value: Name: units Node Type: Element Name: Node Type: TextValue: volts Name: units Node Type: EndElement Name: Node Type: Whitespace Value: Name: instrum Node Type: EndElement Name: Node Type: Whitespace Value: Name: instrum Node Type: Element Name: Node Type: Whitespace Value: Name: name Node Type: Element Name: Node Type: TextValue: Watt Meter..

<?xml version="1.0"?> <instruments>

<instrum><name>DVM</name><units>Volts</units>

</instrum><instrum>

<name>Watt Meter</name><units>Watts</units>

</instrum></instruments>

Page 37: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Name: xml Node Type: XmlDeclaration Value: version="1.0" encoding="utf-8"Name: instruments Node Type: Element Name: instrum Node Type: Element Name: name Node Type: Element Name: Node Type: Text Value: DVM Name: name Node Type: EndElement Name: units Node Type: Element Name: Node Type: Text Value: volts Name: units Node Type: EndElement Name: instrum Node Type: EndElement Name: instrum Node Type: Element Name: name Node Type: Element Name: Node Type: Text Value: Watt Meter Name: name Node Type: EndElement Name: units Node Type: Element Name: Node Type: Text Value: Watts Name: units Node Type: EndElement Name: instrum Node Type: EndElement Name: instruments Node Type: EndElement ..

<?xml version="1.0"?> <instruments>

<instrum><name>DVM</name><units>Volts</units>

</instrum><instrum>

<name>Watt Meter</name><units>Watts</units>

</instrum></instruments>

It can be seen that there are a good deal of whitespace data types. This is because an XML reader can either be set to read whitespace (where it has some significance), or, more typically, where it has no significance. To ignore whitespace the following property is set:xtr.WhitespaceHandling=WhitespaceHandling.None;

Page 38: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

The XmlNode class is used to represent a node in an XML document. Its main attributes is XmlNodeType which defines the type of node. This is a useful way of filtering the XML file. Its main enumerates are:

XmlNodeType.Attribute.XmlNodeType.CDATA.XmlNodeType.Comment.XmlNodeType.Document.XmlNodeType.Element.XmlNodeType.Entity.XmlNodeType.EndelementXmlNodeType.None.XmlNodeType.Text.XmlNodeType.Whitespace. XmlNodeType.XmlDeclaration.

Page 39: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

StringBuilder sbNode = new StringBuilder();XmlTextReader xtr = new XmlTextReader(@"..\..\instrum.xml");xtr.WhitespaceHandling=WhitespaceHandling.None;while (xtr.Read()){

if (xtr.NodeType== XmlNodeType.Element || xtr.NodeType== XmlNodeType.Text)

{sbNode.Length=0;for (int i=1; i<=xtr.Depth; i++){

sbNode.Append(" ");}sbNode.Append("Name: " + xtr.Name + " ");sbNode.Append("Node Type: " + xtr.NodeType.ToString()+ " ");if (xtr.HasValue){

sbNode.Append("Value: " + xtr.Value);}

Page 40: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

lbNodes.Items.Add(sbNode.ToString());str=str+sbNode.ToString()+"\n";if (xtr.HasAttributes){

sbNode.Length=0;for (int i=0;i<xtr.Depth;i++){

sbNode.Append(" ");}sbNode.Append(xtr.Name + " ");sbNode.Append(xtr.NodeType.ToString());if (xtr.HasValue){

sbNode.Append(": " + xtr.Value);}lbNodes.Items.Add(sbNode.ToString());

}}

}xtr.Close();

}

Page 41: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Name: instruments Node Type: Element Name: instrum Node Type: Element Name: name Node Type: Element Name: Node Type: Text Value: DVM Name: units Node Type: Element Name: Node Type: Text Value: Volts Name: instrum Node Type: Element Name: name Node Type: Element Name: Node Type: Text Value: Watt Meter Name: units Node Type: Element Name: Node Type: Text Value: Watts..

<?xml version="1.0"?> <instruments>

<instrum><name>DVM</name><units>Volts</units>

</instrum><instrum>

<name>Watt Meter</name><units>Watts</units>

</instrum></instruments>

Page 42: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

XML Document

Bill Buchanan

Page 43: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

<?xml version="1.0" encoding="UTF-8" ?><!-- Instrument list for Agilent --><Instruments>

<Instrument InstumentNumber="1"><InstrumentName>ABC01</InstrumentName><InstrumentType>RF Meter</InstrumentType><InstrumentRating>1mW</InstrumentRating>

</Instrument><Instrument InstrumentNumber="2">

<InstrumentName>DEF02</InstrumentName><InstrumentType>DVM</InstrumentType><InstrumentRating>10V</InstrumentRating>

</Instrument></Instruments>

Page 44: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void Button1_Click(object sender, System.EventArgs e){XmlTextReader xtr = new XmlTextReader("..\\..\\agilent.xml");xtr.WhitespaceHandling = WhitespaceHandling.None;XmlDocument xd = new XmlDocument();// Load the file into the XmlDocumentxd.Load(xtr);// Add an item representing the document to the ListBoxlbNodes.Items.Add("XML Document");// Find the root node, and add it together with its childrenXmlNode xnod = xd.DocumentElement;AddWithChildren(xnod, 1);

}

Page 45: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void AddWithChildren(XmlNode xnod, Int32 intLevel){

// Adds a node to the ListBox, together with its children. // intLevel controls the depth of indentingXmlNode xnodWorking;String strIndent = new String(' ', 2 * intLevel);// Get the value of the node (if any)String strValue= (String) xnod.Value;if(strValue != null){strValue = " : " + strValue;

}// Add the node details to the ListBoxlbNodes.Items.Add(strIndent + xnod.Name + strValue);// For an element node, retrieve the attributesif(xnod.NodeType == XmlNodeType.Element){XmlNamedNodeMap mapAttributes= xnod.Attributes;// Add the attributes to the ListBox ..

Page 46: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

foreach(XmlNode xnodAttribute in mapAttributes){lbNodes.Items.Add(strIndent + " " +

xnodAttribute.Name + " : " + xnodAttribute.Value);

}// If there are any child nodes,

// call this procedure recursivelyif(xnod.HasChildNodes){xnodWorking = xnod.FirstChild;while (xnodWorking != null){

AddWithChildren(xnodWorking, intLevel + 1);xnodWorking = xnodWorking.NextSibling;

}}

}}

Page 47: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Root

Child

Child Child Child

Child Child

AddWithChildren()

AddWithChildren()

Root.FirstChild

Root.NextSibling

Page 48: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Root

Root

Child Child Child

Child Child

AddWithChildren()

AddWithChildren()

Root.FirstChild

Root.NextSibling

Page 49: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Root

Child

Child Child Child

Root Child

AddWithChildren()

AddWithChildren()

Page 50: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Page 51: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Tutorial Session:

Page 10 and 11

Page 52: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

Loading XML into a DataSet

Bill Buchanan

Page 53: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

XmlTextReader xtr = new XmlTextReader(@"..\..\instrum.xml");XmlDataDocument xdd = new XmlDataDocument();DataSet ds = xdd.DataSet;ds.ReadXmlSchema(xtr);xtr.Close();

xtr = new XmlTextReader(@"..\..\instrum.xml");xdd.Load(xtr);

dgXML.DataSource = ds;dgXML.DataMember="Instrument";

}

Page 54: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Page 55: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

Reading XML from a Schema

Bill Buchanan

Page 56: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

DataSet ds = new DataSet();ds.ReadXmlSchema(@"..\..\instrum.xsd");XmlDataDocument xdd = new XmlDataDocument(ds);xdd.Load(@"..\..\instrum.xml");dgXML.DataSource = ds;dgXML.DataMember="Instrument";

}

Page 57: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

<?xml version="1.0" ?><xs:schema id="Instruments" targetNamespace="http://tempuri.org/instrum.xsd" xmlns:mstns="http://tempuri.org/instrum.xsd"

xmlns="http://tempuri.org/instrum.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"

attributeFormDefault="qualified" elementFormDefault="qualified"><xs:element name="Instruments" msdata:IsDataSet="true" msdata:Locale="en-GB"

msdata:EnforceConstraints="False"><xs:complexType>

<xs:choice maxOccurs="unbounded"><xs:element name="Instrument">

<xs:complexType><xs:sequence><xs:element name="InstrumentName" type="xs:string" /><xs:element name="InstrumentType" type="xs:string" /><xs:element name="InstrumentRating" type="xs:string" />

</xs:sequence><xs:attribute name="InstrumentNumber" type="xs:string" />

</xs:complexType></xs:element>

</xs:choice></xs:complexType>

</xs:element></xs:schema>

Page 58: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Tutorial Session:

Page 15 and 16

Page 59: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

XPath

Bill Buchanan

Page 60: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

The System.Xml.XPath namespace is used to access the XPath parser and evaluation engine. It is particularly useful in searching for elements in the XML document. XPath is basically a query language, where the nodes in the document can be quickly searched for a given query. It can thus be seen as similar to an SQL query on a database, and searches from a current content. These context are:

• ./ - This defines the current node of the document as the current context.

• / - This defines the root of the document as the current context.• .// - This defines the whole document below the current context.• // - This defines the entire XML document as the current

context.

Page 61: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Then

/Readings/Reading

will selected all the readings, and:

/Readings/Reading/InstrumentName

will select all the instrument names. It is possible to also use a wildcard for the context, such as:

/Readings/*/InstrumentName

will implement the equivalent of the previous example.

<?xml version="1.0" encoding="UTF-8" ?><!-- Readings for Agilent --><Readings>

<Reading><InstrumentName>ABC01</InstrumentName><Voltage>10</Voltage><Time>13:10</Time>

</Reading><Reading>

<InstrumentName>ABC01</InstrumentName><Voltage>12</Voltage><Time>13:11</Time>

</Reading><Reading>

<InstrumentName>ABC01</InstrumentName><Voltage>15</Voltage><Time>13:12</Time>

</Reading></Readings>

Page 62: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

XmlDocument xd;xd=readXML(@"..\..\instrum.xml");XmlNodeList xnl = xd.DocumentElement.SelectNodes("//Instruments//Instrument//InstrumentName");showNodes(xnl);

}

Page 63: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void showNodes(XmlNodeList xnl){

lbNodes.Items.Clear();foreach (XmlNode xnod in xnl){

lbNodes.Items.Add(xnod.NodeType.ToString() + ":" +xnod.Name + " = " + xnod.FirstChild.Value);

}}private XmlDocument readXML(String file){

XmlTextReader xtr = new XmlTextReader(file);xtr.WhitespaceHandling=WhitespaceHandling.None;XmlDocument xd = new XmlDocument();xd.Load(xtr);return(xd);

} The sample output of this program is:Element: InstrumentName = ABC01Element: InstrumentName = DEF02

Page 64: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Tutorial Session:

Page 19 and 20

Page 65: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

Generating and Using XSD Schema

Bill Buchanan

Page 66: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Generating a XSD

• Within the designer. This involves generating the XSD from an XML file.• Using the GetXmlSchema() method. For this a DataSet is filled from the

XMLTextReader, after which the GetXml() method is used to display the XML file, and GetXmlSchema() is used to generate the XSD file (XML schema), as given in C# Code 2.6.

• Using the WriteXmlSchema() and ReadXmlSchema() methods. If the XML schema is in the form of an XML file, then the following reads in the schema (with the ReadXmlSchema() method), and exports it (with the WriteXmlSchema() method). See C# Code 2.7.

• Using the InferXmlSchema() method on a dataset. See pp 138 (Kalani, 2003). This is where the format of the XSD is inferred by the XML file.

• Generating the XSD file by hand.

Page 67: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

StringWriter sw = new StringWriter();DataSet ds= XMLtoDS("..\\..\\instrum.xml");tbXML.Text = ds.GetXml();tbXSD.Text = ds.GetXmlSchema();

}private DataSet XMLtoDS(String file){

XmlTextReader xtr = new XmlTextReader(file);xtr.WhitespaceHandling=WhitespaceHandling.None;DataSet ds = new DataSet();

ds.ReadXml(xtr);xtr.Close();return(ds);

}

Page 68: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

StringWriter sw = new StringWriter();DataSet ds= XMLtoDS("..\\..\\instrum.xml");tbXML.Text = ds.GetXml();tbXSD.Text = ds.GetXmlSchema();

}private DataSet XMLtoDS(String file){

XmlTextReader xtr = new XmlTextReader(file);xtr.WhitespaceHandling=WhitespaceHandling.None;DataSet ds = new DataSet();

ds.ReadXml(xtr);xtr.Close();return(ds);

}

Page 69: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

StringWriter sw = new StringWriter();DataSet ds= XMLtoDS("..\\..\\agilent.xml");ds.WriteXmlSchema(sw);tbXSD.Text = sw.ToString();

}private DataSet XMLtoDS(String file){

XmlTextReader xtr = new XmlTextReader(file);xtr.WhitespaceHandling=WhitespaceHandling.None;DataSet ds = new DataSet();

ds.ReadXmlSchema(xtr);xtr.Close();return(ds);

}

Page 70: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

StringWriter sw = new StringWriter();DataSet ds= XMLtoDS("..\\..\\agilent.xml");ds.WriteXmlSchema(sw);tbXSD.Text = sw.ToString();

}private DataSet XMLtoDS(String file){

XmlTextReader xtr = new XmlTextReader(file);xtr.WhitespaceHandling=WhitespaceHandling.None;DataSet ds = new DataSet();

ds.ReadXmlSchema(xtr);xtr.Close();return(ds);

}

Page 71: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

StringWriter sw = new StringWriter();

XmlTextReader xtr = new XmlTextReader(“..”);xtr.WhitespaceHandling=WhitespaceHandling.None;DataSet ds = new DataSet();

ds.WriteXmlSchema(sw);String ns[]= {};// Infer from XML file ds.InterXmlSchema(xtr,ns);

ds.WriteXmlSchema(sw);

tbXSD.Text = sw.ToString();}

Page 72: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

Validating using XSD

Bill Buchanan

Page 73: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

A key objective of the XSD is to validate in the data within the XML file. The main class used for this is XmlValidatingReader. It validates both the XML file and its contents against the XSD.

Page 74: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

XmlTextReader xtr = new XmlTextReader("..\\..\\instrum.xml");

XmlValidatingReader xvr = new XmlValidatingReader(xtr);xvr.ValidationType = ValidationType.Schema;xvr.ValidationEventHandler +=

new ValidationEventHandler(ValidationHandler);XmlDocument xd = new XmlDocument();xd.Load(xvr);xvr.Close();

}public void ValidationHandler(object sender,

ValidationEventArgs e){

tbBox1.AppendText(e.Message + "\n");}

Page 75: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

Validating using DTD

Bill Buchanan

Page 76: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

The DTD (Data Document Definition) is an older standard than using a schema.

<!ELEMENT Readings (Reading)* ><!ELEMENT Reading (InstrumentName, Voltage, Time)><!ELEMENT InstrumentName (#PCDATA)><!ELEMENT Voltage (#PCDATA)><!ELEMENT Time (#PCDATA)> <?xml version="1.0" encoding="UTF-8" ?>

<!-- Readings for Agilent --><!DOCTYPE Readings SYSTEM “instrum.dtd”><Readings>

<Reading><InstrumentName>ABC01</InstrumentName><Voltage>10</Voltage><Time>13:10</Time>

</Reading><Reading>

<InstrumentName>ABC01</InstrumentName><Voltage>12</Voltage><Time>13:11</Time>

</Reading><Reading>

<InstrumentName>ABC01</InstrumentName><Voltage>15</Voltage><Time>13:12</Time>

</Reading></Readings>

Page 77: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

XmlTextReader xtr = new XmlTextReader("..\\..\\instrum.xml");

XmlValidatingReader xvr = new XmlValidatingReader(xtr);xvr.ValidationType = ValidationType.DTD;xvr.ValidationEventHandler +=

new ValidationEventHandler(ValidationHandler);XmlDocument xd = new XmlDocument();xd.Load(xvr);xvr.Close();

}public void ValidationHandler(object sender,

ValidationEventArgs e){

tbBox1.AppendText(e.Message + "\n");}

Page 78: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

Validating using DTD

Bill Buchanan

Page 79: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Tutorial Session:

Page 28 and 29

Page 80: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

Generating XML from SQL

Bill Buchanan

Page 81: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

select companyname, city, countryfrom customerswhere (country='GERMANY') and (city<>'BERLIN')

Page 82: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

select companyname, city, countryfrom customerswhere (country='GERMANY') and (city<>'BERLIN')for xml raw

Page 83: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

<row companyname="Drachenblut Delikatessen" city="Aachen" country="Germany"/><row companyname="Königlich Essen" city="Brandenburg" country="Germany"/><row companyname="QUICK-Stop" city="Cunewalde" country="Germany"/>

Page 84: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

select companyname, city, countryfrom customerswhere (country='GERMANY') and (city<>'BERLIN')for xml auto, elements

Page 85: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

<row companyname="Drachenblut Delikatessen" city="Aachen" country="Germany"/><row companyname="Königlich Essen" city="Brandenburg" country="Germany"/><row companyname="QUICK-Stop" city="Cunewalde" country="Germany"/>

<customers><companyname>Drachenblut Delikatessen</companyname><city>Aachen</city><country>Germany</country>

</customers><customers>

<companyname>Königlich Essen</companyname><city>Brandenburg</city><country>Germany</country>

</customers>

for xml auto, elements

for xml raw

Page 86: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

Updating SQL Server withXML

Bill Buchanan

Page 87: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

The SQL Server can be updated using DiffGrams.

-Install SQLXML.

SqlXmlCommand sxc =(“… connection string”);

Sxc.CommandType = SqlXmlCommandType.DiffGram;

Sxc.SchemaPath = “..\\..\\instrum.xsd”;

FileStream fs = new FileStream(“..\\..\\instrum.xsd”);

Sxc.CommandStream = fs;

Used to apply DiffGrams to database

Page 88: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Tutorial Session:

Page 31

Page 89: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Introduction

Cram

Page 90: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

XmlReader: Which XML object is used to provide read-only access to an XML file.

XmlDocument: read and write access is required for an XML document

XmlNode: access is required to the individual nodes in the XML tree.

XmlTextReader: To just read the XML document, and there is no need for access to XPath, or there is no requirement for XSD validation.

XPath/XPathNavigator: For read access for a search on an XML document.

XmlTextReader.Close(). Close file.

XmlTextReader.GetAttribute(). Read attribute.

XmlTextReader.Read(). Read next node.

XmlTextReader.Skip(). Ignore children of current node, and move on..

Page 91: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

XmlReader.Depth. following determines the number of sublevels of an XML node

XmlReader.EOF. End of file?

XmlReader.Name. Name of node.

XmlReader.NodeType. Type of node.

XmlReader.HasValue. Has a value?

Page 92: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

<?xml version="1.0" encoding="utf-8" ?> <instruments>

<instrum><name>DVM</name><units>Volts</units>

</instrum><instrum>

<name>Watt Meter</name><units>Watts</units>

</instrum></instruments>

Element: <Instruments>

Page 93: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

XmlTextReader xtr = new XmlTextReader(@"instrum.xml");xtr.WhitespaceHandling=WhitespaceHandling.None;

Page 94: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE Readings SYSTEM "instrum.dtd"><Readings>

<Reading><InstrumentName>ABC01</InstrumentName><Voltage>10</Voltage><Time>13:10</Time>

</Reading><Reading>

<InstrumentName>ABC01</InstrumentName><Voltage>12</Voltage><Time>13:11</Time>

</Reading><Reading>

<InstrumentName>ABC01</InstrumentName><Voltage>15</Voltage><Time>13:12</Time>

</Reading></Readings>

Page 95: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

<!ELEMENT Readings (Reading)* ><!ELEMENT Reading (InstrumentName, Voltage, Time)><!ELEMENT InstrumentName (#PCDATA)><!ELEMENT Voltage (#PCDATA)><!ELEMENT Time (#PCDATA)>

Page 96: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Generate schema:

Within the designer, by generating it from the XML fileUsing the GetXmlSchema() method, where a DataSet is filled from the XMLTextReader, after which the GetXml() method is used to display the XML file, and GetXmlSchema() is used to generate the XSD file (XML schema)Using the WriteXmlSchema() and ReadXmlSchema() methods, where the XML schema is in the form of an XML file, then it reads in the schema (with the ReadXmlSchema() method), and exports it (with the WriteXmlSchema() method)Using the InferXmlSchema() method on a dataset.

Page 97: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private DataSet XMLtoDS(String file){

XmlTextReader xtr = new XmlTextReader(file);xtr.WhitespaceHandling=WhitespaceHandling.None;DataSet ds = new DataSet();

ds.ReadXml(xtr);xtr.Close();return(ds);

}

Page 98: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

XmlTextReader xtr = new XmlTextReader("instrum.xml");xtr.WhitespaceHandling=WhitespaceHandling.None;DataSet ds = new DataSet();ds.ReadXml(xtr);xtr.Close();tbXML.Text = ds.GetXml();tbXSD.Text = ds.GetXmlSchema();

Page 99: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){XmlTextReader xtr = new XmlTextReader("instrum.xml");XmlValidatingReader xvr = new XmlValidatingReader(xtr);xvr.ValidationType = ValidationType.DTD;xvr.ValidationEventHandler +=

new ValidationEventHandler(ValidationHandler);

XmlDocument xd = new XmlDocument();xd.Load(xvr);xvr.Close();

}

public void ValidationHandler(object sender, ValidationEventArgs e){tbBox1.AppendText(e.Message + "\n");

Page 100: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

FOR XML RAW: modifier is used to generate XML output.

select companyname, city, countryfrom customerswhere (country='GERMANY') and (city<>'BERLIN')for xml raw

<row companyname="Drachenblut Delikatessen" city="Aachen" country="Germany"/><row companyname="Königlich Essen" city="Brandenburg" country="Germany"/><row companyname="QUICK-Stop" city="Cunewalde" country="Germany"/>

Page 101: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

select companyname, city, countryfrom customerswhere (country='GERMANY') and (city<>'BERLIN')

FOR XML AUTO, ELEMENTS

<customers><companyname>Drachenblut Delikatessen</companyname><city>Aachen</city><country>Germany</country>

</customers><customers>

<companyname>Königlich Essen</companyname><city>Brandenburg</city><country>Germany</country>

</customers>

Page 102: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

select companyname, city, countryfrom customerswhere (country='GERMANY') and (city<>'BERLIN')

FOR XML AUTO, EXPLICIT

&lt;Customer cid="C1" name="Janine"&gt; &lt;Order id="O1" date="1/20/1996"&gt; &lt;OrderDetail id="OD1" pid="P1"/&gt; &lt;OrderDetail id="OD2" pid="P2"/&gt; &lt;/Order&gt; &lt;Order id="O2" date="3/29/1997"&gt; ...&lt;/Customer&gt;

Page 103: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

&lt;?xml version="1.0"?&gt;&lt;questions&gt;

&lt;no_questions&gt;15&lt;/no_questions&gt;&lt;debug&gt;off&lt;/debug&gt;&lt;subject&gt;Introduction to Networks&lt;/subject&gt;&lt;time&gt;60&lt;/time&gt;&lt;quest id="000001"&gt;

&lt;title&gt;The binary equivalent of 5BCE hexademical is:&lt;/title&gt;&lt;q1&gt;101101111001111&lt;/q1&gt;&lt;q2&gt;101101111001100&lt;/q2&gt;&lt;q3&gt;101101111000110&lt;/q3&gt;&lt;q4&gt;101101111011110&lt;/q4&gt;&lt;q5&gt;101101111001110&lt;/q5&gt;&lt;correct&gt;q5&lt;/correct&gt;&lt;level&gt;2&lt;/level&gt;

&lt;/quest&gt;&lt;/questions&gt;

Page 104: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

XmlTextReader xtr = new XmlTextReader("quest.xml");xtr.WhitespaceHandling=WhitespaceHandling.None;while (xtr.Read()){

if (xtr.NodeType== XmlNodeType.Element){

if (xtr.Name=="no_questions") {

xtr.Read();lbNodes.Items.Add("Questions :" + xtr.Value);}

}}xtr.Close();

Page 105: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Which method is used to extract an inline schema from an XML file: The ReadXmlSchema() method of the DataSet class

Which method is used to infer a schema from the structure of an XML file:The InferXmlSchema() method of the DataSet class

Page 106: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

What happens with the ReadXmlSchema() method of the DataSet object:Both elements and attributes within the XML document become DataColumn objects in the DataSet object

Which clause is used to map a column into an element:FOR XML EXPLICIT

Which clauses are used to map columns to attributes (select three):FOR XML AUTO, XMLDATAFOR XML AUTOFOR XML RAW

Which object should be used if the user requires to move from node to node:XmlPathNavigator

Which object should be used if the user requires to retrieve a set of nodes, but not for navigating between them:XmlPathExpression

Page 107: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

private void button1_Click(object sender, System.EventArgs e){

XmlTextReader xtr = new XmlTextReader("instrum.xml");XmlValidatingReader xvr = new XmlValidatingReader(xtr);xvr.ValidationType = ValidationType.Schema;xvr.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);

XmlDocument xd = new XmlDocument();xd.Load(xvr);xvr.Close();

}

public void ValidationHandler(object sender, ValidationEventArgs e){

tbBox1.AppendText(e.Message + "\n");}

Page 108: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

What can the XSD.EXE tool be used for (select four):To generate an XML schema from an EXE or DLLTo generate an XML schema from an XML fileTo generate an XML file from an XML schema fileTo generate DataSet classes from an XML schema file

For an XPath search, which will be the fastest to find nodes://Author/*/Author/Books/*/Author/Books/Book/Author

Page 109: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

What happens with the ReadXmlSchema() method of the DataSet object:Both elements and attributes within the XML document become DataColumn objects in the DataSet objectWhich clause is used to map a column into an element:FOR XML EXPLICITWhich clauses are used to map columns to attributes (select three):FOR XML AUTO, XMLDATAFOR XML AUTOFOR XML RAWWhich object should be used if the user requires to move from node to node:XmlPathNavigatorWhich object should be used if the user requires to retrieve a set of nodes, but not for navigating between them:XmlPathExpressionWhich objects only allow forward-only movement from an XML document:XmlReader XmlTextReader

Page 110: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Which of the following are possible when a DiffGram is sent to a database (select two):It deletes a row from a tableIt adds a row to a table

Which of the following operations can only be achieved with an XML Schema file:When updating an SQL database with a DiffGram

Which of the following operations can be achieved with a DTD or an XDR file:When updating an SQL database with a DiffGram

Page 111: Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC

Au

tho

r: B

ill B

ucha

nan

Au

tho

r: B

ill B

ucha

nan

Which objects only allow forward-only movement from an XML document:XmlReaderXmlTextReader

Which of the following are possible when a DiffGram is sent to a database It deletes a row from a tableIt adds a row to a table

Which of the following operations can only be achieved with an XML Schema file:When updating an SQL database with a DiffGram

Which of the following operations can be achieved with a DTD or an XDR file:When updating an SQL database with a DiffGram