Share via


读写 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 类,请参阅管理 XML 文档中的命名空间

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

#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;
};
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 schema = XmlSchema.Read(reader, ValidationCallback);
            schema.Write(Console.Out);
            FileStream file = new FileStream("new.xsd", FileMode.Create, FileAccess.ReadWrite);
            XmlTextWriter xwriter = new XmlTextWriter(file, new UTF8Encoding());
            xwriter.Formatting = Formatting.Indented;
            schema.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);
    }
}
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

该示例使用 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>  

请参阅