XmlNamespaceDeclarationsAttributeとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlNamespaceDeclarationsAttributeの意味・解説 

XmlNamespaceDeclarationsAttribute クラス

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

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field
 Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue,
 AllowMultiple:=False)> _
Public Class XmlNamespaceDeclarationsAttribute
    Inherits Attribute
Dim instance As XmlNamespaceDeclarationsAttribute
[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
/** @attribute AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue,
 AllowMultiple=false) */ 
public class XmlNamespaceDeclarationsAttribute
 extends Attribute
AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue,
 AllowMultiple=false) 
public class XmlNamespaceDeclarationsAttribute
 extends 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" という名前の属性を格納しています。

 <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.Object
   System.Attribute
    System.Xml.Serialization.XmlNamespaceDeclarationsAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlNamespaceDeclarationsAttribute メンバ
System.Xml.Serialization 名前空間

XmlNamespaceDeclarationsAttribute コンストラクタ

XmlNamespaceDeclarationsAttribute クラスの新しいインスタンスを初期化します。

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

Dim instance As New XmlNamespaceDeclarationsAttribute
public XmlNamespaceDeclarationsAttribute ()
public:
XmlNamespaceDeclarationsAttribute ()
public XmlNamespaceDeclarationsAttribute ()
public function XmlNamespaceDeclarationsAttribute
 ()
解説解説

XmlNamespaceDeclarationsAttribute は、XmlSerializerNamespaces オブジェクトを返す対象だけに適用できます。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlNamespaceDeclarationsAttribute クラス
XmlNamespaceDeclarationsAttribute メンバ
System.Xml.Serialization 名前空間

XmlNamespaceDeclarationsAttribute プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ TypeId  派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。 ( Attribute から継承されます。)
参照参照

関連項目

XmlNamespaceDeclarationsAttribute クラス
System.Xml.Serialization 名前空間

XmlNamespaceDeclarationsAttribute メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 ( Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスのハッシュ コードを返します。 ( Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 ( Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 ( Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 ( Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

XmlNamespaceDeclarationsAttribute クラス
System.Xml.Serialization 名前空間

XmlNamespaceDeclarationsAttribute メンバ

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

XmlNamespaceDeclarationsAttribute データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド XmlNamespaceDeclarationsAttribute XmlNamespaceDeclarationsAttribute クラスの新しいインスタンスを初期化します。
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ TypeId  派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。(Attribute から継承されます。)
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 (Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 (Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスのハッシュ コードを返します。 (Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 (Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 (Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

XmlNamespaceDeclarationsAttribute クラス
System.Xml.Serialization 名前空間



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「XmlNamespaceDeclarationsAttribute」の関連用語

XmlNamespaceDeclarationsAttributeのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XmlNamespaceDeclarationsAttributeのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS