ExportOptions 类

定义

表示可为 XsdDataContractExporter 设置的选项。

public ref class ExportOptions
public class ExportOptions
type ExportOptions = class
Public Class ExportOptions
继承
ExportOptions

示例

下面的示例创建 ExportOptions 类的实例,并将某种类型 (Possessions) 添加到 KnownTypes 属性返回的集合中。

XsdDataContractExporter 随后会导出这些类型的架构,包括添加到集合中的 Possessions 类型。

using System;
using System.Xml;
using System.Runtime.Serialization;
using System.Xml.Schema;

public class Program
{
    public static void Main()
    {
        try
        {
            ExportXSD();
        }
        catch (Exception exc)
        {
            Console.WriteLine("Message: {0} StackTrace:{1}", exc.Message, exc.StackTrace);
        }
        finally
        {
            Console.ReadLine();
        }
    }

    static void ExportXSD()
    {
        XsdDataContractExporter exporter = new XsdDataContractExporter();
        if (exporter.CanExport(typeof(Employee)))
        {
            exporter.Export(typeof(Employee));
            Console.WriteLine("number of schemas: {0}", exporter.Schemas.Count);
            Console.WriteLine();
            XmlSchemaSet mySchemas = exporter.Schemas;

            XmlQualifiedName XmlNameValue = exporter.GetRootElementName(typeof(Employee));
            string EmployeeNameSpace = XmlNameValue.Namespace;

            foreach (XmlSchema schema in mySchemas.Schemas(EmployeeNameSpace))
            {
                schema.Write(Console.Out);
            }
        }
    }

    static void GetXmlElementName()
    {
        XsdDataContractExporter myExporter = new XsdDataContractExporter();
        XmlQualifiedName xmlElementName =myExporter.GetRootElementName(typeof(Employee));
        Console.WriteLine("Namespace: {0}", xmlElementName.Namespace);
        Console.WriteLine("Name: {0}", xmlElementName.Name);
        Console.WriteLine("IsEmpty: {0}", xmlElementName.IsEmpty);
    }

    [DataContract(Namespace = "www.Contoso.com/Examples/")]
    public class Employee
    {
        [DataMember]
        public string EmployeeName;
        [DataMember]
        private string ID;
    }
}
Imports System.Collections
Imports System.Xml
Imports System.Runtime.Serialization
Imports System.Xml.Schema

Public Class Program
    Public Shared Sub Main() 
        Try
            ExportXSD()
        Catch exc As Exception
            Console.WriteLine("Message: {0} StackTrace:{1}", exc.Message, exc.StackTrace)
        Finally
            Console.ReadLine()
        End Try
    
    End Sub 
    
    
    Shared Sub ExportXSD() 

        Dim exporter As New XsdDataContractExporter()

        ' Use the ExportOptions to add the Possessions type to the 
        ' collection of KnownTypes. 
        Dim eOptions As New ExportOptions()
        eOptions.KnownTypes.Add(GetType(Possessions))        
        exporter.Options = eOptions

        If exporter.CanExport(GetType(Employee)) Then
            exporter.Export(GetType(Employee))
            Console.WriteLine("number of schemas: {0}", exporter.Schemas.Count)
            Console.WriteLine()
            Dim mySchemas As XmlSchemaSet = exporter.Schemas
            
            Dim XmlNameValue As XmlQualifiedName = _
               exporter.GetRootElementName(GetType(Employee))
            Dim EmployeeNameSpace As String = XmlNameValue.Namespace
            
            Dim schema As XmlSchema
            For Each schema In  mySchemas.Schemas(EmployeeNameSpace)
                schema.Write(Console.Out)
            Next schema
        End If
    
    End Sub 
    
    
    Shared Sub GetXmlElementName() 
        Dim myExporter As New XsdDataContractExporter()
        Dim xmlElementName As XmlQualifiedName = myExporter. _
            GetRootElementName(GetType(Employee))
        Console.WriteLine("Namespace: {0}", xmlElementName.Namespace)
        Console.WriteLine("Name: {0}", xmlElementName.Name)
        Console.WriteLine("IsEmpty: {0}", xmlElementName.IsEmpty)
    
    End Sub 
    
    <DataContract([Namespace] := "www.Contoso.com/Examples/")>  _
    Public Class Employee

        <DataMember()>  _
        Public EmployeeName As String
        <DataMember()>  _
        Private ID As String
        ' This member may return a Possessions type.
        <DataMember> _
        public Miscellaneous As Hashtable 

    End Class 

    <DataContract> _
    Public Class Possessions

        <DataMember> _
        Public ItemName As String
    End Class

End Class

注解

XsdDataContractExporter 用于从类型或程序集中生成 XSD 架构。 还可以使用 XsdDataContractImporter 从架构文档生成.NET Framework代码。

有关导入和导出架构的详细信息,请参阅 架构导入和导出从类导出架构

KnownTypes 使用 DataContractSerializer 属性来包含可能在对象图中读取的类型。 有关数据协定和已知类型的详细信息,请参阅 数据协定已知类型

有关数据协定的详细信息,请参阅使用数据协定

构造函数

ExportOptions()

初始化 ExportOptions 类的新实例。

属性

DataContractSurrogate

获取或设置序列化代理项。

KnownTypes

获取在序列化或反序列化过程中可能会遇到的类型的集合。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅