15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

eading <strong>and</strong> Writing streamed XMl ❘ 911<br />

settings.ValidationType = ValidationType.Schema;<br />

settings.ValidationEventH<strong>and</strong>ler +=<br />

new System.Xml.Schema.ValidationEventH<strong>and</strong>ler(settings_ValidationEventH<strong>and</strong>ler);<br />

XmlReader rdr = XmlReader.Create("books.xml", settings);<br />

while (rdr.Read())<br />

{<br />

if (rdr.NodeType == XmlNodeType.Text)<br />

richTextBox1.AppendText(rdr.Value + "\r\n");<br />

}<br />

}<br />

code download XMLReaderSample.sln<br />

After the XmlReaderSettings object setting is created, the schema books.xsd is added to the<br />

XmlSchemaSet object. The Add method for XmlSchemaSet has four overloads. One takes an XmlSchema<br />

object. The XmlSchema object can be used to create a schema on the fly, without having to create the schema<br />

file on disk. Another overload takes another XmlSchemaSet object as a parameter. Another takes two<br />

string values: the first is the target namespace <strong>and</strong> the other is the URL for the XSD document. If the target<br />

namespace parameter is null, the targetNamespace of the schema will be used. The last overload takes<br />

the targetNamespace as the first parameter as well, but it uses an XmlReader-based object to read in the<br />

schema. The XmlSchemaSet preprocesses the schema before the document to be validated is processed.<br />

After the schema is referenced, the XsdValidate property is set to one of the ValidationType enumeration<br />

values. These valid values are DTD, Schema, or None. If the value selected is set to None, then no validation<br />

will occur.<br />

Because the XmlReader object is being used, if there is a validation problem with the document, it will<br />

not be found until that attribute or element is read by the reader. When the validation failure does<br />

occur, an XmlSchemaValidationException is raised. This exception can be h<strong>and</strong>led in a catch block;<br />

however, h<strong>and</strong>ling exceptions can make controlling the flow of the data difficult. To help with this, a<br />

ValidationEvent is available in the XmlReaderSettings class. This way, the validation failure can be<br />

h<strong>and</strong>led without your having to use exception h<strong>and</strong>ling. The event is also raised by validation warnings,<br />

which do not raise an exception. The ValidationEvent passes in a ValidationEventArgs object that<br />

contains a Severity property. This property determines whether the event was raised by an error or a<br />

warning. If the event was raised by an error, the exception that caused the event to be raised is passed in as<br />

well. There is also a message property. In the example, the message is displayed in a MessageBox.<br />

using the XmlWriter Class<br />

The XmlWriter class allows you write XML to a stream, a file, a StringBuilder, a TextWriter, or<br />

another XmlWriter object. Like XmlTextReader, it does so in a forward-only, noncached manner.<br />

XmlWriter is highly configurable, allowing you to specify such things as whether or not to indent content,<br />

the amount to indent, what quote character to use in attribute values, <strong>and</strong> whether namespaces are<br />

supported. Like the XmlReader, this configuration is done using an XmlWriterSettings object.<br />

Here’s a simple example that shows how the XmlTextWriter class can be used:<br />

private void button9_Click(object sender, EventArgs e)<br />

{<br />

XmlWriterSettings settings = new XmlWriterSettings();<br />

settings.Indent = true;<br />

settings.NewLineOnAttributes = true;<br />

XmlWriter writer = XmlWriter.Create("newbook.xml", settings);<br />

writer.WriteStartDocument();<br />

//Start creating elements <strong>and</strong> attributes<br />

writer.WriteStartElement("book");<br />

writer.WriteAttributeString("genre", "Mystery");<br />

writer.WriteAttributeString("publicationdate", "2001");<br />

writer.WriteAttributeString("ISBN", "123456789");<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!