Accessing and Manipulating XML Data in Microsoft .NET Framework

by Team uCertify
uCertify.com
Wednesday, 23rd August 2006

The XML Schema Object Model

The structure of XML documents is based on rules that are specified in an XML Schema Definition (XSD) file also known as an XML Schema. An XSD file consists of the definitions of elements, attributes, and data types. XML Schema is used to create, define and validate the structure of XML documents. The structure of an XML document can be specified by the names of elements that are used in the XML documents, and the structure and types of elements that must be valid for a specific schema.

An XML schema is an XML file that has an .xsd file name extension and uses valid XML objects to describe the contents of XML document. The structure of the XML document can be created by using simpleType and complexType elements. A simpleType element is used to define built-in data types or existing simple types and contains no element or attribute. A complexType element consists of elements and attributes.

The XML Schema Object Model (SOM) derived from the System.Xml.Schema namespace consists of a set of classes that read the schema definition from a file and also create the schema definition files programmatically. When a schema is created, it needs to be validated and compiled before writing it to a file. The XML SOM can load and save valid XSD schemas from and to files. In-memory schemas can be created by using strongly typed classes. The XmlSchemaCollection class is used to cache and retrieve schemas, and the XmlValidatingReader class is used to validate XML instance documents.

After creating a schema definition file, the XML Schema Object Model is used to edit files, which is similar to the way that the XML DOM uses for editing files. For validating an XSD file, the Compile method of the XmlSchema class is used to verify whether the schema is semantically correct. During the validation process, a validation callback is used if the parser gives an error or a warning, and simultaneously the ValidationEventHandler event is raised for the semantic validation checking of XML Schema.

Additionally, the XmlSchema.Read method is used to load schemas from a file. The XML SOM is also used to read and write XSD language schemas from files or other sources using the XmlTextReader, XmlTextWriter, and XmlSchema classes. The following code example in Visual Basic .NET shows how to read XML Schema from a file and then to write the schema to a new file.

Imports System.IO
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.Text

Class ReadAndWriteFile
Public Shared Sub Main()
Try
Dim readdata As New XmlTextReader("Students.xsd")
Dim readwrite As XmlSchema = XmlSchema.Read(readdata, Nothing)
readwrite.Write(Console.Out)
Console.WriteLine("Exit Now!")
Console.Read()
Dim file1 As New FileStream("NewFile.xsd", FileMode.Create, _ FileAccess.ReadWrite)
Dim xmlWriter As New XmlTextWriter(file1, New UTF8Encoding())
xmlwriter.Formatting = Formatting.Indented
readwrite.Write(xmlwriter)
Catch e As Exception
Console.WriteLine(e)
End Try
End Sub
End Class


The following code example in Visual C# .NET shows how to read XML Schema from a file and then to write the schema to a new file.

using System.IO;
using System;
using System.Xml;
using System.Xml.Schema;
using System.Text;
class ReadAndWriteFile
{
public static void Main()
{
try
{
XmlTextReader readdata = new XmlTextReader("Students.xsd");
XmlSchema readwrite = XmlSchema.Read(readdata, null);
readwrite.Write(Console.Out);
Console.WriteLine("Exit Now!");
Console.Read();
FileStream file1 = new FileStream("NewFile.xsd", FileMode.Create, FileAccess.ReadWrite);
XmlTextWriter xmlWriter = new XmlTextWriter(file1, new UTF8Encoding());
xmlwriter.Formatting = Formatting.Indented;
readwrite.Write(xmlwriter);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}

After creating, reading and writing XML data of an XML document, and creating XML schemas, now comes the use of the XmlValidatingReader class to validate an XML document. The following sub-heading is a description of an XML validation.


Options:
Printer Friendly
Email Friend

About The Author:

uCertify was formed in 1996 with an aim to offer high quality educational training software and services in the field of information technology to its customers. uCertify provides exam preparation solutions for the certification exams of Microsoft, CIW, CompTIA, Oracle, Sun and other leading IT vendors. To know more about uCertify, please visit http://www.ucertify.com/

Developer Categories



Developer Tutorials
ASP
CGI & Perl
CSS
Flash
HTML
Java
JavaScript
MySQL
PHP
Python
XML

Developer Documentation

Developer Tools



Search our Developer Tutorials
  The DevSyndicate Network