XAttribute 建構函式

定義

初始化 XAttribute 類別的新執行個體。

多載

XAttribute(XAttribute)

從另一個 XAttribute 物件初始化 XAttribute 類別的新執行個體。

XAttribute(XName, Object)

使用指定的名稱和值,初始化 XAttribute 類別的新執行個體。

XAttribute(XAttribute)

從另一個 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 類別的新執行個體。

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

例外狀況

namevalue 參數為 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 可以是 Stringdoublefloatdecimal bool 、、 DateTimeTimeSpan 。 如果值為 DateTimeTimeSpan ,則屬性的值會根據 W3C 規格正確格式化。

另請參閱

適用於