XAttribute コンストラクター
定義
XAttribute クラスの新しいインスタンスを初期化します。Initializes a new instance of the XAttribute class.
オーバーロード
XAttribute(XAttribute) |
他の XAttribute オブジェクトから XAttribute クラスの新しいインスタンスを初期化します。Initializes a new instance of the XAttribute class from another XAttribute object. |
XAttribute(XName, Object) |
指定された名前と値から XAttribute クラスの新しいインスタンスを初期化します。Initializes a new instance of the XAttribute class from the specified name and value. |
XAttribute(XAttribute)
他の XAttribute オブジェクトから XAttribute クラスの新しいインスタンスを初期化します。Initializes a new instance of the XAttribute class from another XAttribute object.
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 オブジェクト。An XAttribute object to copy from.
例外
other
パラメーターが null
です。The other
parameter is null
.
例
この例では、XML ツリーの詳細コピーを作成すると、ツリー内の属性の複製ではなくコピーが作成されることを示しています。This example demonstrates that creating a deep copy of an XML tree creates a copy, not a clone, of an attribute in the tree.
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
この例を実行すると、次の出力が生成されます。This example produces the following output:
Creating a deep copy created a new attribute from the original.
注釈
このコンストラクターは、主に XML ツリーの詳細コピーを作成するときに、内部的に使用されます。This constructor is primarily used internally when making a deep copy of an XML tree.
こちらもご覧ください
適用対象
XAttribute(XName, Object)
指定された名前と値から XAttribute クラスの新しいインスタンスを初期化します。Initializes a new instance of the XAttribute class from the specified name and value.
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
パラメーターまたは value
パラメーターが null
です。The name
or value
parameter is null
.
例
次の例では、このコンストラクターを使用して属性を作成します。The following example uses this constructor to create attributes. このメソッドは、コンストラクターに最初の引数として文字列を渡し XAttribute 、その後、暗黙的にオブジェクトに変換され XName ます。It passes strings as the first argument to the XAttribute constructor, which are then implicitly converted to XName objects. 属性が要素に追加されます。The attributes are added to an element.
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)
この例を実行すると、次の出力が生成されます。This example produces the following output:
<Root Att1="Some text" Att2="12.345" Att3="2006-10-06T12:30:00" Att4="1" Att5="2" Att6="3" />
注釈
文字列からへの暗黙的な変換があり XName ます。There is an implicit conversion from string to XName. このコンストラクターの一般的な使用方法は、次のように、新しいを作成するのではなく、最初のパラメーターとして文字列を指定することです XName 。Typical use of this constructor is to specify a string as the first parameter instead of creating a new XName, as follows:
XElement root = new XElement("Root",
new XAttribute("AnAttributeName", "Content")
);
次のように、と文字列を指定して加算演算子オーバーロードを使用してを作成することもでき XNamespace XName ます。You can also use the addition operator overload with an XNamespace and a string to create an XName, as follows:
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XAttribute(aw + "AnAttributeName", "Content")
);
詳細については、「 XML 名前空間の操作」を参照してください。For more information, see Work with XML Namespaces.
これらの同じ方法は Visual Basic でも機能しますが、xml リテラルは XML ツリーを作成するためのより適切な方法です。These same approaches will work for Visual Basic, however XML literals provide a better approach for creating XML trees.
パラメーターには、、、、、、 value
String double
float
decimal
bool
DateTime 、または TimeSpan を指定できます。The value
parameter can be a String, double
, float
, decimal
, bool
, DateTime, or TimeSpan. 値が DateTime またはの場合 TimeSpan 、属性の値は W3C 仕様に従って正しく書式設定されます。If the value is a DateTime or TimeSpan, the value of the attribute is formatted correctly per the W3C specifications.