Microsoft KB Archive/318505

= HOW TO: Validate an XML Document by Using Multiple Schemas in Visual C# .NET =

Article ID: 318505

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q318505



For a Microsoft Visual Basic .NET version of this article, see 317595.

IN THIS TASK
SUMMARY
 * Requirements
 * Create the Book.xsd File
 * Create the Tape.xsd File
 * Create the Mixed.xml File
 * Create a Visual C# .NET Project

REFERENCES



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(&quot;urn:author&quot;, &quot;authors.xsd&quot;); schemaColl.Add(&quot;urn:author&quot;, &quot;names.xsd&quot;); 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:element> </xs:schema> </li> Save the file as C:\Book.xsd.</li></ol>

back to the top

Create the Tape.xsd File
<ol> Open a text editor such as Notepad.</li>  Copy and paste the following code into Notepad: <xs:schema xmlns=&quot;urn:tapestore-schema&quot; targetNamespace=&quot;urn:tapestore-schema&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xs:element name=&quot;tape&quot; type=&quot;xs:string&quot;/> </xs:schema> </li> Save the file as C:\Tape.xsd.</li></ol>

back to the top

Create the Mixed.xml File
<ol> Open a text editor such as Notepad.</li>  Copy and paste the following code into Notepad: <xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns=&quot;urn:dvdstore-schema&quot; targetNamespace=&quot;urn:dvdstore-schema&quot;> <xs:element name=&quot;dvd&quot; type=&quot;xs:string&quot; /> </xs:schema> <pb:book price=&quot;7.99&quot; xmlns:pb=&quot;urn:bookstore-schema&quot;>The Autobiography of Benjamin Franklin</pb:book> <pd:dvd xmlns:pd=&quot;urn:dvdstore-schema&quot;>The Godfather</pd:dvd> <pt:tape xmlns:pt=&quot;urn:tapestore-schema&quot; xsi:schemaLocation=&quot;urn:tapestore-schema tape.xsd&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;>Il Postino</pt:tape> </li> Save the file as C:\Mixed.xml.</li></ol>

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
<ol> Start Visual Studio .NET.</li> On the File menu, point to New, and then click Project.</li> Click Visual C# Projects under Project Types, click Console Application under Templates, and then click OK.</li>  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 description for Class1. ///    class Class1 {       ///         /// The main entry point for the application. ///        [STAThread] static void Main(string[] args) {           ValidationEventHandler eventHandler = new ValidationEventHandler(Class1.ShowCompileErrors); XmlSchemaCollection myschemacoll = new XmlSchemaCollection; XmlValidatingReader vr; FileStream stream; try {               stream = new FileStream(&quot;c:\\Mixed.xml&quot;, FileMode.Open); //Load the XmlValidatingReader. vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);

//Add the schemas to the XmlSchemaCollection object. myschemacoll.Add(&quot;urn:bookstore-schema&quot;, &quot;c:\\Book.xsd&quot;); myschemacoll.Add(&quot;urn:tapestore-schema&quot;, &quot;c:\\tape.xsd&quot;); vr.Schemas.Add(myschemacoll); vr.ValidationType = ValidationType.Schema; while (vr.Read) {               }                Console.WriteLine(&quot;Validation completed&quot;); }           //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(&quot;Validation Error: {0}&quot;, args.Message); }   } }                    </li> When the following message appears in the output window, the XML document is valid against all three of the schemas that it references:

<pre class="fixed_text">Validation completed

</li></ol>

back to the top

<div class="references_section">