读取和写入 XML 架构

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

读取和写入 XML 架构

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

Write方法将 XML 架构写入StreamTextWriterXmlWriter对象,并且可以选择一个XmlNamespaceManager对象作为参数。 XmlNamespaceManager 用于处理在 XML 架构中遇到的命名空间。 有关该 XmlNamespaceManager 类的详细信息,请参阅 XML 文档中的“管理命名空间”。

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

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>

另请参阅