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.
Assembly: System.Xml (in System.Xml.dll)
The XmlSchemaProviderAttribute type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | XmlSchemaProviderAttribute | Initializes a new instance of the XmlSchemaProviderAttribute class, taking the name of the static method that supplies the type's XML schema. |
| Name | Description | |
|---|---|---|
![]() ![]() | IsAny | Gets or sets a value that determines whether the target class is a wildcard, or that the schema for the class has contains only an xs:any element. |
![]() ![]() ![]() | MethodName | Gets the name of the static method that supplies the type's XML schema and the name of its XML Schema data type. |
![]() | TypeId | When implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.) |
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | Equals | Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.) |
![]() ![]() ![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() ![]() ![]() | GetHashCode | Returns the hash code for this instance. (Inherited from Attribute.) |
![]() ![]() ![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | IsDefaultAttribute | When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.) |
![]() ![]() | Match | When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.) |
![]() ![]() ![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() ![]() ![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | _Attribute.GetIDsOfNames | Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.) |
![]() ![]() | _Attribute.GetTypeInfo | Retrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.) |
![]() ![]() | _Attribute.GetTypeInfoCount | Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.) |
![]() ![]() | _Attribute.Invoke | Provides access to properties and methods exposed by an object. (Inherited from 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.
Note |
|---|
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 = "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
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.







Note