XmlSchemaProviderAttribute クラス
アセンブリ: System.Xml (system.xml.dll 内)

<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Interface)> _ Public NotInheritable Class XmlSchemaProviderAttribute Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Interface)] public sealed class XmlSchemaProviderAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Interface)] public ref class XmlSchemaProviderAttribute sealed : public Attribute

XmlSchemaProviderAttribute の主な役目は、Web サービス記述言語ツール (WSDL.exe) から問い合わせがあった場合、または Visual Studio の [Web 参照の追加] 機能を使用する場合に、XmlSchemaExporter クラスがスキーマを返せるようにすることです。型の実際のスキーマの制御は静的メソッドで行うことができます。
![]() |
---|
MethodName プロパティは、リフレクションを介して静的メソッドの名前を返します。実装の必要があるこのメソッドは、単一のパラメータである XmlSchemaSet オブジェクトを使用する必要があります。このメソッドは、このオブジェクトに XmlSchema オブジェクトを読み込みます。またこのメソッドは、データ型を識別する XmlQualifiedName オブジェクトを返す必要もあります。

次の例では、XmlSchemaProviderAttribute をサーバー側のクラスに適用しています。呼び出しを行うと、MethodName プロパティによって名前が指定されているメソッドがスキーマを作成します。この単純な実装では、ディスクから既存のスキーマを読み込みます。ただし、System.Xml.Schema 名前空間にある型を使用すると、必要に応じて、カスタム スキーマも作成できます。
<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
[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(); } }

System.Attribute
System.Xml.Serialization.XmlSchemaProviderAttribute


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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- XmlSchemaProviderAttribute クラスのページへのリンク