XAttribute コンストラクター

定義

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

オーバーロード

XAttribute(XAttribute)

他の XAttribute オブジェクトから XAttribute クラスの新しいインスタンスを初期化します。

XAttribute(XName, Object)

指定された名前と値から XAttribute クラスの新しいインスタンスを初期化します。

XAttribute(XAttribute)

Source:
XAttribute.cs
Source:
XAttribute.cs
Source:
XAttribute.cs

他の XAttribute オブジェクトから XAttribute クラスの新しいインスタンスを初期化します。

public:
 XAttribute(System::Xml::Linq::XAttribute ^ other);
public XAttribute (System.Xml.Linq.XAttribute other);
new System.Xml.Linq.XAttribute : System.Xml.Linq.XAttribute -> System.Xml.Linq.XAttribute
Public Sub New (other As XAttribute)

パラメーター

other
XAttribute

コピー元の XAttribute オブジェクト。

例外

other パラメーターが null です。

この例では、XML ツリーのディープ コピーを作成すると、ツリー内の属性のコピー (複製ではなく) が作成されることを示します。

XElement root1 = XElement.Parse("<Root Att1='abc' />");  
// Make a deep copy.  
XElement root2 = new XElement(root1);  
if (root1.Attribute("Att1") == root2.Attribute("Att1"))  
    Console.WriteLine("This will not be printed");  
else  
    Console.WriteLine("Creating a deep copy created a new attribute from the original.");  
Dim root1 As XElement = <Root Att1='abc'/>  
' Make a deep copy.  
Dim root2 As XElement = New XElement(root1)  
If root1.Attribute("Att1") Is root2.Attribute("Att1") Then  
    Console.WriteLine("This will not be printed")  
Else  
    Console.WriteLine("Creating a deep copy created a new attribute from the original.")  
End If  

この例を実行すると、次の出力が生成されます。

Creating a deep copy created a new attribute from the original.  

注釈

このコンストラクターは、主に XML ツリーのディープ コピーを作成するときに内部的に使用されます。

こちらもご覧ください

適用対象

XAttribute(XName, Object)

Source:
XAttribute.cs
Source:
XAttribute.cs
Source:
XAttribute.cs

指定された名前と値から XAttribute クラスの新しいインスタンスを初期化します。

public:
 XAttribute(System::Xml::Linq::XName ^ name, System::Object ^ value);
public XAttribute (System.Xml.Linq.XName name, object value);
new System.Xml.Linq.XAttribute : System.Xml.Linq.XName * obj -> System.Xml.Linq.XAttribute
Public Sub New (name As XName, value As Object)

パラメーター

name
XName

属性の XName

value
Object

属性の値を含む Object

例外

name パラメーターまたは value パラメーターが null です。

次の例では、このコンストラクターを使用して属性を作成します。 コンストラクターに最初の引数として文字列を XAttribute 渡し、暗黙的にオブジェクトに XName 変換します。 属性は 要素に追加されます。

XElement root;  

double dbl = 12.345;  
XAttribute[] attArray = {  
    new XAttribute("Att4", 1),  
    new XAttribute("Att5", 2),  
    new XAttribute("Att6", 3)  
};  
DateTime dt = new DateTime(2006, 10, 6, 12, 30, 00);  

// string content  
root = new XElement("Root",  
    new XAttribute("Att1", "Some text"),  

    // double content  
    new XAttribute("Att2", dbl),  

    // DateTime content  
    new XAttribute("Att3", dt),  

    // XAttribute array content  
    attArray  
);  

Console.WriteLine(root);  
Dim dbl As Double = 12.345  
Dim attArray As XAttribute() = { _  
    New XAttribute("Att4", 1), _  
    New XAttribute("Att5", 2), _  
    New XAttribute("Att6", 3) _  
}  
Dim dt As DateTime = New DateTime(2006, 10, 6, 12, 30, 0)  
Dim root As XElement = <Root Att1="Some text"  
                           Att2=<%= dbl %>  
                           Att3=<%= dt %>  
                           <%= attArray %>  
                       />  
Console.WriteLine(root)  

この例を実行すると、次の出力が生成されます。

<Root Att1="Some text" Att2="12.345" Att3="2006-10-06T12:30:00" Att4="1" Att5="2" Att6="3" />  

注釈

文字列 XNameから への暗黙的な変換があります。 このコンストラクターの一般的な使用方法は、次のように新しい XNameを作成する代わりに、最初のパラメーターとして文字列を指定することです。

XElement root = new XElement("Root",  
    new XAttribute("AnAttributeName", "Content")  
);  

加算演算子のオーバーロードと 文字列を使用 XNamespace して、次のように を XName作成することもできます。

XNamespace aw = "http://www.adventure-works.com";  
XElement root = new XElement(aw + "Root",  
    new XAttribute(aw + "AnAttributeName", "Content")  
);  

詳細については、「 XML 名前空間の操作」を参照してください。

これらの同じ方法は Visual Basic でも機能しますが、XML リテラルは XML ツリーを作成するためのより優れたアプローチを提供します。

パラメーターにはvaluedecimalStringfloatbooldoubleDateTimeまたは を指定TimeSpanできます。 値が DateTime または TimeSpanの場合、属性の値は W3C 仕様に従って正しく書式設定されます。

こちらもご覧ください

適用対象