Download presentation
Presentation is loading. Please wait.
Published byMargery Barker Modified over 9 years ago
1
Author: Bill Buchanan.NET XML Web Services Alistair Lawson, SoC Bill Buchanan, SoC
2
Author: Bill Buchanan DateTitleKalani Monday 4 June 11-12Unit 0: Introduction to.NET- Monday 4 June 2-4pmUnit 0: Introduction to.NET- Tuesday 5 June 10-12Unit 1: Creating/Manipulating DatasetsUnit 1 Tuesday 5 June 2-4pmUnit 1: Creating/Manipulating DatasetsUnit 1 Monday 11 JuneUnit 2: Manipulating XML DataUnit 2 Tuesday 12 JuneUnit 2: Manipulating XML DataUnit 2 Monday 18 JuneUnit 3:.NET RemotingUnit 3 Tuesday 19 JuneUnit 3:.NET RemotingUnit 3 Monday 25 JuneUnit 4: Web ServicesUnit 4 Tuesday 26 JuneUnit 4: Web ServicesUnit 5
3
Author: Bill Buchanan Accessing and Manipulating XML Alistair Lawson, SoC Bill Buchanan, SoC
4
Author: Bill Buchanan Consuming 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.
5
Author: Bill Buchanan Introduction Bill Buchanan
6
Author: Bill Buchanan 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.
7
Author: Bill Buchanan MS Access My SQL Oracle CSV XML XML Schema Program Binary files Linux Windows OS2 UNIX
8
Author: Bill Buchanan DataSet DataViewDataRow SqlCommand SqlAdapter SqlConnection DataGridListBoxCheckBox BinaryReader BinaryWriter FileStream StreamReader StreamWriter FileStream XmlNode XmlDocument XmlTextReader Data Model Data Store Database Binary fileText fileXML file User Interface SqlReader
9
Author: Bill Buchanan 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 XmlTextReader Data Store Database Binary fileText fileXML file SqlReader
10
Author: Bill Buchanan 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 XmlTextReader Data Store Database Binary fileText fileXML file SqlReader
11
Author: Bill Buchanan Introduction XML Bill Buchanan
12
Author: Bill Buchanan 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:
13
Author: Bill Buchanan 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.
14
Author: Bill Buchanan 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.
15
Author: Bill Buchanan 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.
16
Author: Bill Buchanan Root Children
17
Author: Bill Buchanan Root Child Document root First Child Next Sibling
18
Author: Bill Buchanan Root Child Document root First Child Next Sibling Child
19
Author: Bill Buchanan Root Child Root Child Document root Child
20
Author: Bill Buchanan Introduction XML Elements Bill Buchanan
21
Author: Bill Buchanan 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, 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: Blah blah Blah Blah In this case the XML parser will stop after as the parser expects to find the next.
22
Author: Bill Buchanan 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 <, > and & can be used. For example: If we wanted the contents of a tag to be My then the following would be used: My < Web>
23
Author: Bill Buchanan Attributes An 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”. Smith, Fred XML Developer's Guide Disk tools 100 1.02 Windows XP version
24
Author: Bill Buchanan 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.
25
Author: Bill Buchanan Introduction XML Format Bill Buchanan
26
Author: Bill Buchanan 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: The prolog can include processing instructions, such as the xml-stylesheet processing instruction (as shown in the about example). The statement contains a comment.
27
Author: Bill Buchanan 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: If an element name does not have any attributes then its format is: and the end tag has the following format (which cannot have any attributes):
28
Author: Bill Buchanan Elements are contained between a start and an end tag. For example: Cat Honey where the pet element contains two other elements: and. The tags contains the text ‘ Cat ’, and the 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: which is the same as:
29
Author: Bill Buchanan 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, is the root document: Cat Honey The following cannot be an XML document, as it has no root document: Cat Honey 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.
30
Author: Bill Buchanan 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.
31
Author: Bill Buchanan Introduction.NET XML Toolkit Bill Buchanan
32
Author: Bill Buchanan 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.
33
Author: Bill Buchanan 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.DepthDefines the depth of the XML node XmlTestReader.EOFDefines if the end-of-file marker XmlTextReader.NameGet the name of the node XmlTextReader.NodeTypeGet the type of the node XmlTextReader.HasValueDetermine if the node has a value property
34
Author: Bill Buchanan Code Example 2.1 gives an example of the XmlTextReader implementation, and Sample Run 2.1 shows a test run for the following file: DVM Volts Watt Meter Watts
35
Author: Bill Buchanan 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(); }
36
Author: Bill Buchanan 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.. DVM Volts Watt Meter Watts
37
Author: Bill Buchanan 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.. DVM Volts Watt Meter Watts 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;
38
Author: Bill Buchanan 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.Endelement XmlNodeType.None. XmlNodeType.Text. XmlNodeType.Whitespace. XmlNodeType.XmlDeclaration.
39
Author: Bill Buchanan 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); }
40
Author: Bill Buchanan 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(); }
41
Author: Bill Buchanan 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.. DVM Volts Watt Meter Watts
42
Author: Bill Buchanan Introduction XML Document Bill Buchanan
43
Author: Bill Buchanan ABC01 RF Meter 1mW DEF02 DVM 10V
44
Author: Bill Buchanan 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 XmlDocument xd.Load(xtr); // Add an item representing the document to the ListBox lbNodes.Items.Add("XML Document"); // Find the root node, and add it together with its children XmlNode xnod = xd.DocumentElement; AddWithChildren(xnod, 1); }
45
Author: Bill Buchanan private void AddWithChildren(XmlNode xnod, Int32 intLevel) { // Adds a node to the ListBox, together with its children. // intLevel controls the depth of indenting XmlNode 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 ListBox lbNodes.Items.Add(strIndent + xnod.Name + strValue); // For an element node, retrieve the attributes if(xnod.NodeType == XmlNodeType.Element) { XmlNamedNodeMap mapAttributes= xnod.Attributes; // Add the attributes to the ListBox..
46
Author: Bill Buchanan foreach(XmlNode xnodAttribute in mapAttributes) { lbNodes.Items.Add(strIndent + " " + xnodAttribute.Name + " : " + xnodAttribute.Value); } // If there are any child nodes, // call this procedure recursively if(xnod.HasChildNodes) { xnodWorking = xnod.FirstChild; while (xnodWorking != null) { AddWithChildren(xnodWorking, intLevel + 1); xnodWorking = xnodWorking.NextSibling; }
47
Author: Bill Buchanan Root Child AddWithChildren() Root.FirstChild Root.NextSibling
48
Author: Bill Buchanan Root Child AddWithChildren() Root.FirstChild Root.NextSibling
49
Author: Bill Buchanan Root Child Root Child AddWithChildren()
50
Author: Bill Buchanan
51
Tutorial Session: Page 10 and 11
52
Author: Bill Buchanan Introduction Loading XML into a DataSet Bill Buchanan
53
Author: Bill Buchanan 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"; }
54
Author: Bill Buchanan
55
Introduction Reading XML from a Schema Bill Buchanan
56
Author: Bill Buchanan 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"; }
57
Author: Bill Buchanan <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">
58
Author: Bill Buchanan Tutorial Session: Page 15 and 16
59
Author: Bill Buchanan Introduction XPath Bill Buchanan
60
Author: Bill Buchanan 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.
61
Author: Bill Buchanan 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. ABC01 10 13:10 ABC01 12 13:11 ABC01 15 13:12
62
Author: Bill Buchanan 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); }
63
Author: Bill Buchanan 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 = ABC01 Element: InstrumentName = DEF02
64
Author: Bill Buchanan Tutorial Session: Page 19 and 20
65
Author: Bill Buchanan Introduction Generating and Using XSD Schema Bill Buchanan
66
Author: Bill Buchanan 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.
67
Author: Bill Buchanan 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); }
68
Author: Bill Buchanan 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); }
69
Author: Bill Buchanan 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); }
70
Author: Bill Buchanan 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); }
71
Author: Bill Buchanan 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(); }
72
Author: Bill Buchanan Introduction Validating using XSD Bill Buchanan
73
Author: Bill Buchanan 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.
74
Author: Bill Buchanan 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"); }
75
Author: Bill Buchanan Introduction Validating using DTD Bill Buchanan
76
Author: Bill Buchanan The DTD (Data Document Definition) is an older standard than using a schema. ABC01 10 13:10 ABC01 12 13:11 ABC01 15 13:12
77
Author: Bill Buchanan 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"); }
78
Author: Bill Buchanan Introduction Validating using DTD Bill Buchanan
79
Author: Bill Buchanan Tutorial Session: Page 28 and 29
80
Author: Bill Buchanan Introduction Generating XML from SQL Bill Buchanan
81
Author: Bill Buchanan select companyname, city, country from customers where (country='GERMANY') and (city<>'BERLIN')
82
Author: Bill Buchanan select companyname, city, country from customers where (country='GERMANY') and (city<>'BERLIN') for xml raw
83
Author: Bill Buchanan
84
Author: Bill Buchanan select companyname, city, country from customers where (country='GERMANY') and (city<>'BERLIN') for xml auto, elements
85
Author: Bill Buchanan Drachenblut Delikatessen Aachen Germany Königlich Essen Brandenburg Germany for xml auto, elements for xml raw
86
Author: Bill Buchanan Introduction Updating SQL Server with XML Bill Buchanan
87
Author: Bill Buchanan 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
88
Author: Bill Buchanan Tutorial Session: Page 31
89
Author: Bill Buchanan Introduction Cram
90
Author: Bill Buchanan 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..
91
Author: Bill Buchanan 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?
92
Author: Bill Buchanan DVM Volts Watt Meter Watts Element:
93
Author: Bill Buchanan XmlTextReader xtr = new XmlTextReader(@"instrum.xml"); xtr.WhitespaceHandling=WhitespaceHandling.None;
94
Author: Bill Buchanan ABC01 10 13:10 ABC01 12 13:11 ABC01 15 13:12
95
Author: Bill Buchanan
96
Author: Bill Buchanan Generate schema: Within the designer, by generating it from the XML file Using 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.
97
Author: Bill Buchanan 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); }
98
Author: Bill Buchanan 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();
99
Author: Bill Buchanan 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");
100
Author: Bill Buchanan FOR XML RAW: modifier is used to generate XML output. select companyname, city, country from customers where (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"/>
101
Author: Bill Buchanan select companyname, city, country from customers where (country='GERMANY') and (city<>'BERLIN') FOR XML AUTO, ELEMENTS Drachenblut Delikatessen Aachen Germany Königlich Essen Brandenburg Germany
102
Author: Bill Buchanan select companyname, city, country from customers where (country='GERMANY') and (city<>'BERLIN') FOR XML AUTO, EXPLICIT <Customer cid="C1" name="Janine"> <Order id="O1" date="1/20/1996"> <OrderDetail id="OD1" pid="P1"/> <OrderDetail id="OD2" pid="P2"/> </Order> <Order id="O2" date="3/29/1997">... </Customer>
103
Author: Bill Buchanan <?xml version="1.0"?> <questions> <no_questions>15</no_questions> <debug>off</debug> <subject>Introduction to Networks</subject> <time>60</time> <quest id="000001"> <title>The binary equivalent of 5BCE hexademical is:</title> <q1>101101111001111</q1> <q2>101101111001100</q2> <q3>101101111000110</q3> <q4>101101111011110</q4> <q5>101101111001110</q5> <correct>q5</correct> <level>2</level> </quest> </questions>
104
Author: Bill Buchanan 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();
105
Author: Bill Buchanan 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
106
Author: Bill Buchanan 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, XMLDATA FOR XML AUTO FOR 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
107
Author: Bill Buchanan 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"); }
108
Author: Bill Buchanan What can the XSD.EXE tool be used for (select four): To generate an XML schema from an EXE or DLL To generate an XML schema from an XML file To generate an XML file from an XML schema file To 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
109
Author: Bill Buchanan 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, XMLDATA FOR XML AUTO FOR 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 Which objects only allow forward-only movement from an XML document: XmlReader XmlTextReader
110
Author: Bill Buchanan Which of the following are possible when a DiffGram is sent to a database (select two): It deletes a row from a table It 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
111
Author: Bill Buchanan Which objects only allow forward-only movement from an XML document: XmlReader XmlTextReader Which of the following are possible when a DiffGram is sent to a database It deletes a row from a table It 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.