XAttribute Construtores
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Inicializa uma nova instância da XAttribute classe.
Sobrecargas
| Name | Description |
|---|---|
| XAttribute(XAttribute) |
Inicializa uma nova instância da XAttribute classe a partir de outro XAttribute objeto. |
| XAttribute(XName, Object) |
Inicializa uma nova instância da XAttribute classe a partir do nome e valor especificados. |
XAttribute(XAttribute)
- Origem:
- XAttribute.cs
- Origem:
- XAttribute.cs
- Origem:
- XAttribute.cs
- Origem:
- XAttribute.cs
- Origem:
- XAttribute.cs
Inicializa uma nova instância da XAttribute classe a partir de outro XAttribute objeto.
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)
Parâmetros
- other
- XAttribute
Um XAttribute objeto para copiar.
Exceções
O other parâmetro é null.
Exemplos
Este exemplo demonstra que criar uma cópia profunda de uma árvore XML cria uma cópia, não um clone, de um atributo na árvore.
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
Este exemplo produz a seguinte saída:
Creating a deep copy created a new attribute from the original.
Observações
Este construtor é usado principalmente internamente ao fazer uma cópia profunda de uma árvore XML.
Ver também
Aplica-se a
XAttribute(XName, Object)
- Origem:
- XAttribute.cs
- Origem:
- XAttribute.cs
- Origem:
- XAttribute.cs
- Origem:
- XAttribute.cs
- Origem:
- XAttribute.cs
Inicializa uma nova instância da XAttribute classe a partir do nome e valor especificados.
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)
Parâmetros
Exceções
O name parâmetro ou value é null.
Exemplos
O exemplo seguinte usa este construtor para criar atributos. Passa cadeias como primeiro argumento ao XAttribute construtor, que são então implicitamente convertidas em XName objetos. Os atributos são adicionados a um elemento.
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)
Este exemplo produz a seguinte saída:
<Root Att1="Some text" Att2="12.345" Att3="2006-10-06T12:30:00" Att4="1" Att5="2" Att6="3" />
Observações
Existe uma conversão implícita de cadeia para XName. O uso típico deste construtor é especificar uma cadeia como primeiro parâmetro em vez de criar um novo XName, da seguinte forma:
XElement root = new XElement("Root",
new XAttribute("AnAttributeName", "Content")
);
Também pode usar o operador de adição overload com uma XNamespace string e para criar um XName, da seguinte forma:
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XAttribute(aw + "AnAttributeName", "Content")
);
Para mais informações, consulte Trabalhar com Namespaces XML.
Estas mesmas abordagens funcionam para o Visual Basic, no entanto, os literais XML fornecem uma melhor abordagem para criar árvores XML.
O value parâmetro pode ser um String, double, float, decimal, bool, DateTime, ou TimeSpan. Se o valor for a DateTime ou TimeSpan, o valor do atributo está corretamente formatado conforme as especificações do W3C.