XAttribute 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 XAttribute 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| 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或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參數可以是 String、 double、 floatdecimalboolDateTimeTimeSpan或 。 若值為 或 DateTimeTimeSpan,屬性值依 W3C 規範正確格式化。