次の方法で共有


XmlNamespaceDeclarationsAttribute クラス

対象となるプロパティ、パラメータ、戻り値、またはクラス メンバに、XML ドキュメント内で使用する、名前空間に関連付けられたプリフィックスを含めることを指定します。

この型のすべてのメンバの一覧については、XmlNamespaceDeclarationsAttribute メンバ を参照してください。

System.Object
   System.Attribute
      System.Xml.Serialization.XmlNamespaceDeclarationsAttribute

<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field _
   Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue)>
Public Class XmlNamespaceDeclarationsAttribute   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field
   | AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class XmlNamespaceDeclarationsAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Property |
   AttributeTargets::Field | AttributeTargets::Parameter |
   AttributeTargets::ReturnValue)]
public __gc class XmlNamespaceDeclarationsAttribute : public   Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Property | AttributeTargets.Field |
   AttributeTargets.Parameter | AttributeTargets.ReturnValue)
class XmlNamespaceDeclarationsAttribute extends Attribute

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

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" という名前の属性を格納しています。

<select xmlns:cal ="http://www.cohowinery.com/calendar/" path="cal:appointments/@startTime" />

これに対するスキーマは次のようになります。

<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 要素を表すクラスに追加します。

<?xml version="1.0"?>
<x:root xmlns:x="http://www.cohowinery.com/x/">
  <x:select xmlns:cal="http://www.cohowinery.com/calendar/" path="cal:appointments/@cal:startTime" />
</x:root>

必要条件

名前空間: System.Xml.Serialization

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: System.Xml (System.Xml.dll 内)

参照

XmlNamespaceDeclarationsAttribute メンバ | System.Xml.Serialization 名前空間