XAttribute.ToString 方法

定義

將目前的 XAttribute 物件轉換為字串表示。

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

String

String,包含屬性的 XML 文字表示和其值。

範例

下列範例會建立具有數個屬性的專案。 然後它會取得屬性,並藉由呼叫此方法來顯示內容及其內容。

XElement root = new XElement("Root",  
    new XAttribute("Att1", "content1"),  
    new XAttribute("Att2", "content2"),  
    new XAttribute("Att3", "content3")  
);  
XAttribute att = root.Attribute("Att2");  
Console.WriteLine(att.ToString());  
Dim root As XElement = <Root Att1="content1" Att2="content2" Att3="content3"/>  
Dim att As XAttribute = root.Attribute("Att2")  
Console.WriteLine(att.ToString())  

這個範例會產生下列輸出:

Att2="content2"  

備註

這個方法會以 的形式 AttributeName="content" 傳回字串。

警告

呼叫此方法並 XAttribute 包含控制字元時,會擲回例外狀況,訊息如下:十六進位值0x0B為無效字元。

適用於

另請參閱