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)
| Name | Description | |
|---|---|---|
![]() | XmlSchemaProviderAttribute(String) | 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 |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | This API supports the product infrastructure and is not intended to be used directly from your code. Returns a value that indicates whether this instance is equal to a specified object.(Inherited from Attribute.) |
![]() | GetHashCode() | Returns the hash code for this instance.(Inherited from Attribute.) |
![]() | GetType() | |
![]() | 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(Object) | When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.(Inherited from Attribute.) |
![]() | ToString() | Returns a string that represents the current object.(Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from Attribute.) |
![]() ![]() | _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | Retrieves the type information for an object, which can be used to get the type information for an interface.(Inherited from Attribute.) |
![]() ![]() | _Attribute.GetTypeInfoCount(UInt32) | Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from Attribute.) |
![]() ![]() | _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 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.
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
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.




