SUMMARY
This step-by-step article describes how to use the
XmlValidatingReader object to validate an Extensible Markup Language (XML) file with multiple XML Schema Definition Language (XSD) schemas. The code sample uses the
XmlSchemaCollection object to cache the schemas. For more information about the
XmlValidatingReader and the
XmlSchemaCollection classes, see the
REFERENCES section.
If the
namespace is already associated with another schema in the collection, the schema that you add replaces the original schema in the collection. For example, the following code removes the Authors.xsd file from the collection and adds the Names.xsd file to the collection:
schemaColl.Add("urn:author", "authors.xsd");
schemaColl.Add("urn:author", "names.xsd");
The XML file is always validated against the last schema if the schemas have the same namespace.
back to the top
Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you are required:
- Microsoft Visual Studio .NET installed on a compatible Microsoft Windows operating system.
This article assumes that you are familiar with the following topics:
- Microsoft Visual C# .NET
- XML standards
- XSD schemas
back to the top
Create the Book.xsd File
- Open a text editor such as Notepad.
- Copy and paste the following code into Notepad:
<xs:schema xmlns="urn:bookstore-schema"
targetNamespace="urn:bookstore-schema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="price" type="xs:decimal" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
- Save the file as C:\Book.xsd.
back to the top
Create the Tape.xsd File
- Open a text editor such as Notepad.
- Copy and paste the following code into Notepad:
<xs:schema xmlns="urn:tapestore-schema"
targetNamespace="urn:tapestore-schema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tape" type="xs:string"/>
</xs:schema>
- Save the file as C:\Tape.xsd.
back to the top
Create the Mixed.xml File
- Open a text editor such as Notepad.
- Copy and paste the following code into Notepad:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:dvdstore-schema" targetNamespace="urn:dvdstore-schema">
<xs:element name="dvd" type="xs:string" />
</xs:schema>
<pb:book price="7.99" xmlns:pb="urn:bookstore-schema">The Autobiography of Benjamin Franklin</pb:book>
<pd:dvd xmlns:pd="urn:dvdstore-schema">The Godfather</pd:dvd>
<pt:tape xmlns:pt="urn:tapestore-schema" xsi:schemaLocation="urn:tapestore-schema tape.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Il Postino</pt:tape>
- Save the file as C:\Mixed.xml.
NOTE: The XML file also has an inline schema; therefore, this XML file must validate against three schemas.
back to the top
Create a Visual C# .NET Project
- Start Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- Click Visual C# Projects under Project Types, click Console Application under Templates, and then click OK.
- Replace the code in Class1.cs with the following code:
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
namespace ConsoleApplication5
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ValidationEventHandler eventHandler = new ValidationEventHandler(Class1.ShowCompileErrors);
XmlSchemaCollection myschemacoll = new XmlSchemaCollection();
XmlValidatingReader vr;
FileStream stream;
try
{
stream = new FileStream("c:\\Mixed.xml", FileMode.Open);
//Load the XmlValidatingReader.
vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);
//Add the schemas to the XmlSchemaCollection object.
myschemacoll.Add("urn:bookstore-schema", "c:\\Book.xsd");
myschemacoll.Add("urn:tapestore-schema", "c:\\tape.xsd");
vr.Schemas.Add(myschemacoll);
vr.ValidationType = ValidationType.Schema;
while (vr.Read())
{
}
Console.WriteLine("Validation completed");
}
//This code catches any XML exceptions.
catch (XmlException XmlExp)
{
Console.WriteLine(XmlExp.Message);
}
//This code catches any XML schema exceptions.
catch (XmlSchemaException XmlSchemaExp)
{
Console.WriteLine(XmlSchemaExp.Message);
}
//This code catches any standard exceptions.
catch (Exception GeneralExp)
{
Console.WriteLine(GeneralExp.Message);
}
finally
{
//Clean up.
Console.Read();
vr = null;
myschemacoll = null;
stream = null;
}
}
public static void ShowCompileErrors(object sender, ValidationEventArgs args)
{
Console.WriteLine("Validation Error: {0}", args.Message);
}
}
}
- When the following message appears in the output window, the XML document is valid against all three of the schemas that it references:
back to the top