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 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「XmlNamespaceDeclarationsAttribute クラス」の関連用語

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

   

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



XmlNamespaceDeclarationsAttribute クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS