读写 XML 架构

架构对象模型 (SOM) API 可以用于从文件或其他源读取和写入 XML 架构定义语言 (XSD) 架构并使用 System.Xml.Schema 命名空间中的类生成内存中 XML 架构,这些架构映射到万维网联合会 (W3C) XML 架构建议中定义的结构。

读取和写入 XML 架构

XmlSchema 类提供 ReadWrite 方法来读取和写入 XML 架构。 Read 方法返回表示 XML 架构的 XmlSchema 对象并使用可选的 ValidationEventHandler 作为参数,以处理在读取 XML 架构时遇到的架构验证警告和错误。

Write 方法将 XML 架构写入 StreamTextWriterXmlWriter 对象,并且可以使用可选的 XmlNamespaceManager 对象作为参数。 XmlNamespaceManager 用于处理在 XML 架构中遇到的命名空间。 有关 XmlNamespaceManager 类的更多信息,请参见使用 XmlNamespaceManager 管理命名空间

以下代码示例说明如何在文件中读取和写入 XML 架构。 代码示例使用 example.xsd 文件,使用 static Read 方法将其读入 XmlSchema 对象,然后将文件写入控制台和新的 new.xsd 文件。 代码示例还将 ValidationEventHandler 作为参数传递给 static Read 方法,以处理在读取 XML 架构时遇到的任何架构验证警告或错误。 如果未指定 ValidationEventHandler (null),则不会报告任何警告或错误。

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

Class XmlSchemaReadWriteExample

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

            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
            myschema.Write(xwriter)
        Catch e As Exception
            Console.WriteLine(e)
        End Try
    End Sub

    Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs)
        If args.Severity = XmlSeverityType.Warning Then
            Console.Write("WARNING: ")
        Else
            If args.Severity = XmlSeverityType.Error Then
                Console.Write("ERROR: ")
            End If
        End If
        Console.WriteLine(args.Message)
    End Sub
End Class
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;

class XmlSchemaReadWriteExample
{
    static void Main()
    {
        try
        {
            XmlTextReader reader = new XmlTextReader("example.xsd");
            XmlSchema myschema = XmlSchema.Read(reader, ValidationCallback);
            myschema.Write(Console.Out);
            FileStream file = new FileStream("new.xsd", FileMode.Create, FileAccess.ReadWrite);
            XmlTextWriter xwriter = new XmlTextWriter(file, new UTF8Encoding());
            xwriter.Formatting = Formatting.Indented;
            myschema.Write(xwriter);
        }
        catch(Exception e)
        {
            Console.WriteLine(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: ");

        Console.WriteLine(args.Message);
    }
}
#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
{
public:

    static void Main()
    {
        try
        {
            XmlTextReader^ reader = gcnew XmlTextReader("example.xsd");
            ValidationEventHandler^ eventHandler = gcnew ValidationEventHandler(ValidationCallback);
            XmlSchema^ myschema = XmlSchema::Read(reader, eventHandler);
            myschema->Write(Console::Out);
            FileStream^ file = gcnew FileStream("new.xsd", FileMode::Create, FileAccess::ReadWrite);
            XmlTextWriter^ xwriter = gcnew XmlTextWriter(file, gcnew UTF8Encoding());
            xwriter->Formatting = Formatting::Indented;
            myschema->Write(xwriter);
        }
        catch(Exception^ e)
        {
            Console::WriteLine(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: ");

        Console::WriteLine(args->Message);
    }
};

int main()
{
    XmlSchemaReadWriteExample::Main();
    return 0;
};

该示例使用 example.xsd 作为输入。

<?xml version="1.0"?>
<xs:schema id="play" targetNamespace="http://tempuri.org/play.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/play.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name='myShoeSize'>
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base='xs:decimal'>
                    <xs:attribute name='sizing' type='xs:string' />
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>
</xs:schema>

请参见

概念

XML 架构对象模型概述

生成 XML 架构

遍历 XML 架构

编辑 XML 架构

包含或导入 XML 架构

用于编译架构的 XmlSchemaSet

后架构编译信息集

使用 XmlNamespaceManager 管理命名空间