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

<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, AllowMultiple:=False)> _ Public Class XmlNamespaceDeclarationsAttribute Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=false)] public class XmlNamespaceDeclarationsAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Property|AttributeTargets::Field|AttributeTargets::Parameter|AttributeTargets::ReturnValue, AllowMultiple=false)] public ref class XmlNamespaceDeclarationsAttribute : public Attribute

XmlNamespaceDeclarationsAttribute 属性は、XmlSerializerNamespaces オブジェクトを返すフィールドまたはプロパティに対して、クラスで一度だけ適用できます。
XmlNamespaceDeclarationsAttribute によって、XML ドキュメントで使用するプリフィックスや関連付けられた名前空間を格納できます。たとえば、この属性の一般的な使用法の 1 つは、World Wide Web Consortium (www.w3.org) ドキュメントの「XML Language (XPath) Version 1.0」に定義されている XPath データを格納することです。XPath は、簡単に言えば、多くの名前空間プレフィックスやローカル名と他の構文を一緒に含む文字列です。
XPath 言語によって、プリフィックスをパスに関連付け、そのプリフィックスを XML ドキュメント内で使用できます。たとえば、次の "select" という名前の XML ドキュメントには、特定の URI (http://www.cohowinery.com/calendar/) に関連付けられたプリフィックス ("cal") が含まれています。要素は、XPath を含む "path" という名前の属性を格納しています。
<element name="select"> <complexType> <simpleContent> <attribute name="path" /> </simpleContent> </complexType> </element>
XmlNamespaceDeclarationsAttribute がない場合は、プレフィックスと名前空間の関連付けは失われます。
プリフィックスと名前空間 URI の関連付けを保持するには、次の C# コードと Visual Basic コードに示すように、XmlSerializerNamespaces オブジェクトを返すメンバを追加し、XmlNamespaceDeclarationsAttribute 属性をそのメンバに適用します。
// C# public class Select { [XmlAttribute] public string path; [XmlNamespaceDeclarations] public XmlSerializerNamespaces xmlns; } ' Visual Basic Public Class Select <XmlAttribute> Public path As String <XmlNamespaceDeclarations> Public xmlns As XmlSerializerNamespaces End Class
シリアル化すると、生成された XML ドキュメントのスキーマは、appinfo という名前の XML スキーマ定義 (XSD) 要素を格納します。さらに、この要素は、名前空間宣言を含むメンバの名前に設定される keepNamespaceDeclarations という名前のメタデータ要素を含みます。次の XML フラグメントはこのスキーマを示しています。
<xs:element name="select"> <xs:complexType> <xs:annotation> <xs:appinfo> <keepNamespaceDeclarations>xmlns</keepNamespaceDeclarations> </xs:appinfo> </xs:annotation> <xs:simpleContent> <xs:attribute name="path" /> </xs:simpleContent> </xs:complexType> </xs:element>
逆シリアル化では、xmlns フィールドには、すべての名前空間プレフィックスの定義を含む XmlSerializerNamespaces オブジェクトが格納されます。
シリアル化では、Add メソッドを使用して、プリフィックスと名前空間のペアを XmlSerializerNamespaces オブジェクトに追加できます。これを C# コードと Visual Basic コードで次に示します。
// C# using System; using System.IO; using System.Xml.Serialization; [XmlRoot("select")] public class Select { [XmlAttribute] public string xpath; [XmlNamespaceDeclarations] public XmlSerializerNamespaces xmlns; } public class Test { public static void Main(string[] args) { Select mySelect = new Select(); mySelect.xpath = "myNS:ref/@common:y"; mySelect.xmlns = new XmlSerializerNamespaces(); mySelect.xmlns.Add("MyNS", "myNS.tempuri.org"); mySelect.xmlns.Add("common", "common.tempuri.org"); XmlSerializer ser = new XmlSerializer(typeof(Select)); ser.Serialize(Console.Out, mySelect); } } // Output: // <?xml version="1.0" encoding="IBM437"?> // <select xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" // xmlns:common="common.tempuri.org" xmlns:MyNS="myNS.tempuri.org" xpath="myNS:ref/@common:y" /> ' Visual Basic Imports System Imports System.IO Imports System.Xml.Serialization <XmlRoot("select")> _ Public Class SelectPath <XmlAttribute> _ Public xpath As String <XmlNamespaceDeclarations> _ public xmlns As XmlSerializerNamespaces End Class Public Class Test Public Shared Sub Main() Dim mySelect As SelectPath = New SelectPath() mySelect.xpath = "myNS:ref/@common:y" mySelect.xmlns = New XmlSerializerNamespaces() mySelect.xmlns.Add("MyNS", "myNS.tempuri.org") mySelect.xmlns.Add("common", "common.tempuri.org") Dim ser As XmlSerializer = New XmlSerializer(mySelect.GetType) ser.Serialize(Console.Out, mySelect) End Sub End Class 'Output: ' <?xml version="1.0" encoding="IBM437"?> ' <select xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' xmlns:common="common.tempuri.org" xmlns:MyNS="myNS.tempuri.org" xpath="myNS:ref/@common:y" />
属性が適用されるメンバには、クラスで定義された XML 要素に属するプレフィックスと名前空間のペア以外は格納されないため注意が必要です。たとえば、次の XML ドキュメントでは、プレフィックス ペア "cal" だけがキャプチャされ、"x" プレフィックスはキャプチャされません。このデータを取得するには、XmlNamespaceDeclarationsAttribute を含むメンバを root 要素を表すクラスに追加します。

System.Attribute
System.Xml.Serialization.XmlNamespaceDeclarationsAttribute


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


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