Reading and Writing XML Schemas

The Schema Object Model (SOM) API can be used to read and write XML Schema definition language (XSD) schemas from files or other sources and build XML schemas in-memory using the classes in the System.Xml.Schema namespace that map to the structures defined in the World Wide Web Consortium (W3C) XML Schema Recommendation.

Reading and Writing XML Schemas

The XmlSchema class provides the Read and Write methods to read and write XML schemas. The Read method returns an XmlSchema object representing the XML schema and takes an optional ValidationEventHandler as a parameter to handle schema validation warnings and errors encountered while reading an XML schema.

The Write method writes XML schemas to Stream, TextWriter and XmlWriter objects and can take an optional XmlNamespaceManager object as a parameter. An XmlNamespaceManager is used to handle namespaces encountered in an XML schema. For more information about the XmlNamespaceManager class, see Managing Namespaces in an XML Document.

The following code example illustrates reading and writing XML schemas from and to a file. The code example takes the example.xsd file, reads it into an XmlSchema object using the staticRead method, and then writes the file to the console and a new new.xsd file. The code example also provides a ValidationEventHandler as a parameter to the staticRead method to handle any schema validation warnings or errors encountered while reading the XML schema. If the ValidationEventHandler is not specified (null), no warnings or errors are reported.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Xml;
using namespace System::Xml::Schema;

ref class XmlSchemaReadWriteExample

    static void Main()
            XmlTextReader^ reader = gcnew XmlTextReader("example.xsd");
            ValidationEventHandler^ eventHandler = gcnew ValidationEventHandler(ValidationCallback);
            XmlSchema^ myschema = XmlSchema::Read(reader, eventHandler);
            FileStream^ file = gcnew FileStream("new.xsd", FileMode::Create, FileAccess::ReadWrite);
            XmlTextWriter^ xwriter = gcnew XmlTextWriter(file, gcnew UTF8Encoding());
            xwriter->Formatting = Formatting::Indented;
        catch(Exception^ e)

    static void ValidationCallback(Object^ sender, ValidationEventArgs^ args)
        if (args->Severity == XmlSeverityType::Warning)
            Console::Write("WARNING: ");
        else if (args->Severity == XmlSeverityType::Error)
            Console::Write("ERROR: ");


int main()
    return 0;
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;

class XmlSchemaReadWriteExample
    static void Main()
            XmlTextReader reader = new XmlTextReader("example.xsd");
            XmlSchema schema = XmlSchema.Read(reader, ValidationCallback);
            FileStream file = new FileStream("new.xsd", FileMode.Create, FileAccess.ReadWrite);
            XmlTextWriter xwriter = new XmlTextWriter(file, new UTF8Encoding());
            xwriter.Formatting = Formatting.Indented;
        catch(Exception e)

    static void ValidationCallback(object sender, ValidationEventArgs args)
        if (args.Severity == XmlSeverityType.Warning)
            Console.Write("WARNING: ");
        else if (args.Severity == XmlSeverityType.Error)
            Console.Write("ERROR: ");

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

Class XmlSchemaReadWriteExample

    Shared Sub Main()
            Dim reader As XmlTextReader = New XmlTextReader("example.xsd")
            Dim myschema As XmlSchema = XmlSchema.Read(reader, AddressOf ValidationCallback)

            Dim file As FileStream = New FileStream("new.xsd", FileMode.Create, FileAccess.ReadWrite)
            Dim xwriter As XmlTextWriter = New XmlTextWriter(file, New UTF8Encoding())
            xwriter.Formatting = Formatting.Indented
        Catch e As Exception
        End Try
    End Sub

    Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs)
        If args.Severity = XmlSeverityType.Warning Then
            Console.Write("WARNING: ")
            If args.Severity = XmlSeverityType.Error Then
                Console.Write("ERROR: ")
            End If
        End If
    End Sub
End Class

The example takes the example.xsd as input.

<?xml version="1.0"?>  
<xs:schema id="play" targetNamespace="" elementFormDefault="qualified" xmlns="" xmlns:xs="">  
    <xs:element name='myShoeSize'>  
                <xs:extension base='xs:decimal'>  
                    <xs:attribute name='sizing' type='xs:string' />  

See also