XmlElement.SetAttributeNode Metoda

Definicja

Dodaje nowy XmlAttributeelement .

Przeciążenia

SetAttributeNode(XmlAttribute)

Dodaje określony XmlAttributeelement .

SetAttributeNode(String, String)

Dodaje określony XmlAttributeelement .

SetAttributeNode(XmlAttribute)

Źródło:
XmlElement.cs
Źródło:
XmlElement.cs
Źródło:
XmlElement.cs

Dodaje określony XmlAttributeelement .

public:
 virtual System::Xml::XmlAttribute ^ SetAttributeNode(System::Xml::XmlAttribute ^ newAttr);
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

Parametry

newAttr
XmlAttribute

Węzeł do dodania XmlAttribute do kolekcji atrybutów dla tego elementu.

Zwraca

Jeśli atrybut zastępuje istniejący atrybut o tej samej nazwie, jest zwracany stary XmlAttribute ; w przeciwnym razie null jest zwracany.

Wyjątki

Obiekt newAttr został utworzony na podstawie innego dokumentu niż ten, który utworzył ten węzeł. Lub ten węzeł jest tylko do odczytu.

Element newAttr jest już atrybutem innego XmlElement obiektu. Aby ponownie używać węzłów w innych XmlElement obiektach, należy jawnie sklonować XmlAttribute węzły.

Uwagi

Jeśli atrybut o tej nazwie jest już obecny w elemecie , zostanie zastąpiony przez nowy.

Dotyczy

SetAttributeNode(String, String)

Źródło:
XmlElement.cs
Źródło:
XmlElement.cs
Źródło:
XmlElement.cs

Dodaje określony XmlAttributeelement .

public:
 virtual System::Xml::XmlAttribute ^ SetAttributeNode(System::String ^ localName, System::String ^ namespaceURI);
public virtual System.Xml.XmlAttribute SetAttributeNode (string localName, 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

Parametry

localName
String

Lokalna nazwa atrybutu.

namespaceURI
String

Identyfikator URI przestrzeni nazw atrybutu.

Zwraca

Element do dodania XmlAttribute .

Przykłady

Poniższy przykład dodaje atrybut do elementu.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew 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 );
}
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

Uwagi

Nie XmlAttribute ma żadnych dzieci. Służy Value do przypisywania wartości tekstowej do atrybutu lub użycia AppendChild (lub podobnej metody) w celu dodania elementów podrzędnych do atrybutu.

Dotyczy