Bewerken

Delen via


XAttribute Constructors

Definition

Initializes a new instance of the XAttribute class.

Overloads

XAttribute(XAttribute)

Initializes a new instance of the XAttribute class from another XAttribute object.

XAttribute(XName, Object)

Initializes a new instance of the XAttribute class from the specified name and value.

XAttribute(XAttribute)

Source:
XAttribute.cs
Source:
XAttribute.cs
Source:
XAttribute.cs

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)

Parameters

other
XAttribute

An XAttribute object to copy from.

Exceptions

The other parameter is null.

Examples

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.  

Remarks

This constructor is primarily used internally when making a deep copy of an XML tree.

See also

Applies to

XAttribute(XName, Object)

Source:
XAttribute.cs
Source:
XAttribute.cs
Source:
XAttribute.cs

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)

Parameters

name
XName

The XName of the attribute.

value
Object

An Object containing the value of the attribute.

Exceptions

The name or value parameter is null.

Examples

The following example uses this constructor to create attributes. 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" />  

Remarks

There is an implicit conversion from string to 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")  
);  

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")  
);  

For more information, see Work with XML Namespaces.

These same approaches will work for Visual Basic, however XML literals provide a better approach for creating XML trees.

The value parameter can be a String, double, float, decimal, bool, DateTime, or TimeSpan. If the value is a DateTime or TimeSpan, the value of the attribute is formatted correctly per the W3C specifications.

See also

Applies to