XmlSchemaProviderAttribute Klass

Definition

När det tillämpas på en typ lagrar du namnet på en statisk metod av den typ som returnerar ett XML-schema och en XmlQualifiedName (eller XmlSchemaType för anonyma typer) som styr serialiseringen av typen.

public ref class XmlSchemaProviderAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)]
public sealed class XmlSchemaProviderAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Struct)>]
type XmlSchemaProviderAttribute = class
    inherit Attribute
Public NotInheritable Class XmlSchemaProviderAttribute
Inherits Attribute
Arv
XmlSchemaProviderAttribute
Attribut

Exempel

Följande exempel gäller för en serverdelsklass XmlSchemaProviderAttribute . När den anropas skapar metoden som heter av MethodName egenskapen schemat. Den här enkla implementeringen läser ett befintligt schema utanför disken. Men du kan också skapa ett anpassat schema, om det behövs, med hjälp av de typer som finns i System.Xml.Schema namnområdet.

[XmlSchemaProvider("MySchema")]
public class SongStream : IXmlSerializable
{
    private const string ns = "http://demos.Contoso.com/webservices";
    private string filePath;

    public SongStream() { }

    public SongStream(string filePath)
    {
        this.filePath = filePath;
    }

    // This is the method named by the XmlSchemaProviderAttribute applied to the type.
    public static XmlQualifiedName MySchema(XmlSchemaSet xs)
    {
        // This method is called by the framework to get the schema for this type.
        // We return an existing schema from disk.

        XmlSerializer schemaSerializer = new XmlSerializer(typeof(XmlSchema));
        string xsdPath = null;
        // NOTE: replace the string with your own path.
        xsdPath = System.Web.HttpContext.Current.Server.MapPath("SongStream.xsd");
        XmlSchema s = (XmlSchema)schemaSerializer.Deserialize(
            new XmlTextReader(xsdPath), null);
        xs.XmlResolver = new XmlUrlResolver();
        xs.Add(s);

        return new XmlQualifiedName("songStream", ns);
    }

    void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer)
    {
        // This is the chunking code.
        // ASP.NET buffering must be turned off for this to work.

        int bufferSize = 4096;
        char[] songBytes = new char[bufferSize];
        FileStream inFile = File.Open(this.filePath, FileMode.Open, FileAccess.Read);

        long length = inFile.Length;

        // Write the file name.
        writer.WriteElementString("fileName", ns, Path.GetFileNameWithoutExtension(this.filePath));

        // Write the size.
        writer.WriteElementString("size", ns, length.ToString());

        // Write the song bytes.
        writer.WriteStartElement("song", ns);

        StreamReader sr = new StreamReader(inFile, true);
        int readLen = sr.Read(songBytes, 0, bufferSize);

        while (readLen > 0)
        {
            writer.WriteStartElement("chunk", ns);
            writer.WriteChars(songBytes, 0, readLen);
            writer.WriteEndElement();

            writer.Flush();
            readLen = sr.Read(songBytes, 0, bufferSize);
        }

        writer.WriteEndElement();
        inFile.Close();
    }

    System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
    {
        throw new System.NotImplementedException();
    }

    void IXmlSerializable.ReadXml(System.Xml.XmlReader reader)
    {
        throw new System.NotImplementedException();
    }
}
<XmlSchemaProvider("MySchema")>  _
Public Class SongStream
    Implements IXmlSerializable
    
    Private Const ns As String = "http://demos.Contoso.com/webservices"
    Private filePath As String
    
    Public Sub New() 
    
    End Sub
     
    Public Sub New(ByVal filePath As String) 
        Me.filePath = filePath
    End Sub
    
    
    ' This is the method named by the XmlSchemaProviderAttribute applied to the type.
    Public Shared Function MySchema(ByVal xs As XmlSchemaSet) As XmlQualifiedName 
        ' This method is called by the framework to get the schema for this type.
        ' We return an existing schema from disk.
        Dim schemaSerializer As New XmlSerializer(GetType(XmlSchema))
        Dim xsdPath As String = Nothing
        ' NOTE: replace SongStream.xsd with your own schema file.
        xsdPath = System.Web.HttpContext.Current.Server.MapPath("SongStream.xsd")
        Dim s As XmlSchema = CType(schemaSerializer.Deserialize(New XmlTextReader(xsdPath)), XmlSchema)
        xs.XmlResolver = New XmlUrlResolver()
        xs.Add(s)
        
        Return New XmlQualifiedName("songStream", ns)
    
    End Function
    
    
    
    Sub WriteXml(ByVal writer As System.Xml.XmlWriter)  Implements IXmlSerializable.WriteXml
        ' This is the chunking code.
        ' ASP.NET buffering must be turned off for this to work.
        
        Dim bufferSize As Integer = 4096
        Dim songBytes(bufferSize) As Char
        Dim inFile As FileStream = File.Open(Me.filePath, FileMode.Open, FileAccess.Read)
        
        Dim length As Long = inFile.Length
        
        ' Write the file name.
        writer.WriteElementString("fileName", ns, Path.GetFileNameWithoutExtension(Me.filePath))
        
        ' Write the size.
        writer.WriteElementString("size", ns, length.ToString())
        
        ' Write the song bytes.
        writer.WriteStartElement("song", ns)
        
        Dim sr As New StreamReader(inFile, True)
        Dim readLen As Integer = sr.Read(songBytes, 0, bufferSize)
        
        While readLen > 0
            writer.WriteStartElement("chunk", ns)
            writer.WriteChars(songBytes, 0, readLen)
            writer.WriteEndElement()
            
            writer.Flush()
            readLen = sr.Read(songBytes, 0, bufferSize)
        End While
        
        writer.WriteEndElement()
        inFile.Close()
    End Sub 
        
    Function GetSchema() As System.Xml.Schema.XmlSchema  Implements IXmlSerializable.GetSchema
        Throw New System.NotImplementedException()
    End Function
    
    Sub ReadXml(ByVal reader As System.Xml.XmlReader)  Implements IXmlSerializable.ReadXml
        Throw New System.NotImplementedException()
    End Sub 
End Class

Kommentarer

Huvudsyftet med XmlSchemaProviderAttribute är att göra det möjligt för klassen XmlSchemaExporter att returnera ett schema vid frågor från verktyget Web Services Description Language (WSDL.exe), eller när du använder Lägg till webbreferens funktion i Visual Studio. Du kan styra det faktiska schemat för typen i den statiska metoden.

Note

Attributets målklass måste implementera IXmlSerializable gränssnittet.

Egenskapen MethodName returnerar namnet på en statisk metod genom reflektion. Metoden, som måste implementeras, måste ha en enda parameter, ett XmlSchemaSet objekt, som metoden fyller i med ett XmlSchema objekt. Metoden måste också returnera ett XmlQualifiedName objekt som identifierar datatypen.

Returnera anonyma typer

Det går inte att skapa en anonym komplex typ med hjälp av en metod som returnerar XmlQualifiedName. Eftersom en anonym typ inte har något namn och du inte kan lägga till en anonym typ i ett schema måste den anonyma typen returneras som en XmlSchemaType.

Konstruktorer

Name Description
XmlSchemaProviderAttribute(String)

Initierar en ny instans av XmlSchemaProviderAttribute klassen med namnet på den statiska metod som tillhandahåller typens XML-schema.

Egenskaper

Name Description
IsAny

Hämtar eller anger ett värde som avgör om målklassen är ett jokertecken eller om schemat för klassen bara innehåller ett xs:any element.

MethodName

Hämtar namnet på den statiska metod som tillhandahåller typens XML-schema och namnet på dess XML-schemadatatyp.

TypeId

När den implementeras i en härledd klass hämtar du en unik identifierare för den här Attribute.

(Ärvd från Attribute)

Metoder

Name Description
Equals(Object)

Returnerar ett värde som anger om den här instansen är lika med ett angivet objekt.

(Ärvd från Attribute)
GetHashCode()

Returnerar hash-koden för den här instansen.

(Ärvd från Attribute)
GetType()

Hämtar den aktuella instansen Type .

(Ärvd från Object)
IsDefaultAttribute()

När den åsidosättas i en härledd klass anger du om värdet för den här instansen är standardvärdet för den härledda klassen.

(Ärvd från Attribute)
Match(Object)

När den åsidosätts i en härledd klass returneras ett värde som anger om den här instansen är lika med ett angivet objekt.

(Ärvd från Attribute)
MemberwiseClone()

Skapar en ytlig kopia av den aktuella Object.

(Ärvd från Object)
ToString()

Returnerar en sträng som representerar det aktuella objektet.

(Ärvd från Object)

Explicita gränssnittsimplementeringar

Name Description
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Mappar en uppsättning namn till en motsvarande uppsättning av sändningsidentifierare.

(Ärvd från Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Hämtar typinformationen för ett objekt, som kan användas för att hämta typinformationen för ett gränssnitt.

(Ärvd från Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Hämtar antalet typinformationsgränssnitt som ett objekt tillhandahåller (antingen 0 eller 1).

(Ärvd från Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Ger åtkomst till egenskaper och metoder som exponeras av ett objekt.

(Ärvd från Attribute)

Gäller för

Se även