This documentation is archived and is not being maintained.

XmlSchemaProviderAttribute Class

When applied to a type, stores the name of a static method of the type that returns an XML schema and a XmlQualifiedName (or XmlSchemaType for anonymous types) that controls the serialization of the type.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)

<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Interface)> _
Public NotInheritable Class XmlSchemaProviderAttribute _
	Inherits Attribute
Dim instance As XmlSchemaProviderAttribute

The main purpose of the XmlSchemaProviderAttribute is to enable the XmlSchemaExporter class to return a schema when queried by the Web Services Description Language tool (WSDL.exe), or when using the Add Web Reference feature of Visual Studio. You can control the actual schema for the type in the static method.


The target class of the attribute must implement the IXmlSerializable interface.

The MethodName property returns the name of a static method through reflection. The method, which must be implemented, must take a single parameter, an XmlSchemaSet object, which the method populates with an XmlSchema object. The method must also return an XmlQualifiedName object that identifies the data type.

Returning Anonymous Types

An anonymous complex type cannot be created using a method that returns XmlQualifiedName. Because an anonymous type has no name, and you cannot add an anonymous type to a schema, the anonymous type must be returned as an XmlSchemaType.

The following example applies the XmlSchemaProviderAttribute to a server-side class. When invoked, the method named by the MethodName property creates the schema. This simple implementation reads an existing schema off the disk. However, you can also create a custom schema, if necessary, using the types found in the System.Xml.Schema namespace.

<XmlSchemaProvider("MySchema")>  _
Public Class SongStream
    Implements IXmlSerializable

    Private Const ns As String = "" 
    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()

        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)

            readLen = sr.Read(songBytes, 0, bufferSize)
        End While

    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


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0