XAttribute コンストラクター

定義

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

オーバーロード

名前 説明
XAttribute(XAttribute)

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

XAttribute(XName, Object)

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

XAttribute(XAttribute)

ソース:
XAttribute.cs
ソース:
XAttribute.cs
ソース:
XAttribute.cs
ソース:
XAttribute.cs
ソース:
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)

ソース:
XAttribute.cs
ソース:
XAttribute.cs
ソース:
XAttribute.cs
ソース:
XAttribute.cs
ソース:
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 ツリーを作成するためのより優れたアプローチを提供します。

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

こちらもご覧ください

適用対象