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입니다.

예외

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, float``decimal, boolDateTime또는 TimeSpan.일 수 있습니다. 값이 a DateTime 또는 TimeSpan이면 특성의 값은 W3C 사양에 따라 올바르게 서식이 지정됩니다.

추가 정보

적용 대상