XmlElement.SetAttributeNode Método

Definição

Adiciona um novo XmlAttribute.

Sobrecargas

Name Description
SetAttributeNode(XmlAttribute)

Adiciona o especificado XmlAttribute.

SetAttributeNode(String, String)

Adiciona o especificado XmlAttribute.

SetAttributeNode(XmlAttribute)

Adiciona o especificado XmlAttribute.

public:
 virtual System::Xml::XmlAttribute ^ SetAttributeNode(System::Xml::XmlAttribute ^ newAttr);
public virtual System.Xml.XmlAttribute SetAttributeNode(System.Xml.XmlAttribute newAttr);
abstract member SetAttributeNode : System.Xml.XmlAttribute -> System.Xml.XmlAttribute
override this.SetAttributeNode : System.Xml.XmlAttribute -> System.Xml.XmlAttribute
Public Overridable Function SetAttributeNode (newAttr As XmlAttribute) As XmlAttribute

Parâmetros

newAttr
XmlAttribute

O XmlAttribute nó a adicionar à coleção de atributos para este elemento.

Devoluções

Se o atributo substituir um atributo existente com o mesmo nome, o antigo XmlAttribute é devolvido; caso contrário, null é devolvido.

Exceções

Foi newAttr criado a partir de um documento diferente daquele que criou este nó. Ou este nó é só de leitura.

O newAttr já é um atributo de outro XmlElement objeto. Tens de clonar XmlAttribute explicitamente os nós para os reutilizar noutros XmlElement objetos.

Observações

Se um atributo com esse nome já estiver presente no elemento, é substituído pelo novo.

Aplica-se a

SetAttributeNode(String, String)

Adiciona o especificado XmlAttribute.

public:
 virtual System::Xml::XmlAttribute ^ SetAttributeNode(System::String ^ localName, System::String ^ namespaceURI);
public virtual System.Xml.XmlAttribute SetAttributeNode(string localName, string namespaceURI);
abstract member SetAttributeNode : string * string -> System.Xml.XmlAttribute
override this.SetAttributeNode : string * string -> System.Xml.XmlAttribute
Public Overridable Function SetAttributeNode (localName As String, namespaceURI As String) As XmlAttribute

Parâmetros

localName
String

O nome local do atributo.

namespaceURI
String

O URI do namespace do atributo.

Devoluções

O XmlAttribute a acrescentar.

Exemplos

O exemplo seguinte adiciona um atributo a um elemento.

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlElement root = doc.DocumentElement;

    // Add a new attribute.
    XmlAttribute attr = root.SetAttributeNode("genre", "urn:samples");
    attr.Value="novel";

    Console.WriteLine("Display the modified XML...");
    Console.WriteLine(doc.InnerXml);
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" & _
                "<title>Pride And Prejudice</title>" & _
                "</book>")

    Dim root as XmlElement = doc.DocumentElement

    ' Add a new attribute.
    Dim attr as XmlAttribute = root.SetAttributeNode("genre", "urn:samples")
    attr.Value="novel"

    Console.WriteLine("Display the modified XML...")
    Console.WriteLine(doc.InnerXml)

  end sub
end class

Observações

Não XmlAttribute tem filhos. Use Value para atribuir um valor de texto ao atributo ou use AppendChild (ou um método semelhante) para adicionar filhos ao atributo.

Aplica-se a