XmlSchemaProviderAttribute Class

Note: This class is new in the .NET Framework version 2.0.

When applied to a type, stores the name of a static method of the type that returns an XML schema and a XmlQualifiedName that controls the serialization of the type.

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

public sealed class XmlSchemaProviderAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Interface) */ 
public final class XmlSchemaProviderAttribute extends Attribute
public final class XmlSchemaProviderAttribute extends Attribute

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.

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.

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();

     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);

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



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

    void IXmlSerializable.ReadXml(System.Xml.XmlReader reader)
     throw new System.NotImplementedException();



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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0

Community Additions