Bagikan melalui


XmlElement.SetAttributeNode Metode

Definisi

Menambahkan baru XmlAttribute.

Overload

SetAttributeNode(XmlAttribute)

Sumber:
XmlElement.cs
Sumber:
XmlElement.cs
Sumber:
XmlElement.cs
Sumber:
XmlElement.cs
Sumber:
XmlElement.cs

XmlAttributeMenambahkan .

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

Parameter

newAttr
XmlAttribute

Simpul XmlAttribute yang akan ditambahkan ke koleksi atribut untuk elemen ini.

Mengembalikan

Jika atribut mengganti atribut yang ada dengan nama yang sama, atribut lama XmlAttribute dikembalikan; jika tidak, null dikembalikan.

Pengecualian

newAttr Dibuat dari dokumen yang berbeda dari yang membuat simpul ini. Atau simpul ini bersifat baca-saja.

newAttr sudah merupakan atribut dari objek lainXmlElement. Anda harus secara eksplisit mengkloning XmlAttribute simpul untuk menggunakannya kembali di objek lain XmlElement .

Keterangan

Jika atribut dengan nama tersebut sudah ada dalam elemen , atribut tersebut digantikan oleh yang baru.

Berlaku untuk

SetAttributeNode(String, String)

Sumber:
XmlElement.cs
Sumber:
XmlElement.cs
Sumber:
XmlElement.cs
Sumber:
XmlElement.cs
Sumber:
XmlElement.cs

XmlAttributeMenambahkan .

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

Parameter

localName
String

Nama lokal atribut.

namespaceURI
String

URI namespace atribut.

Mengembalikan

yang XmlAttribute akan ditambahkan.

Contoh

Contoh berikut menambahkan atribut ke elemen.

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

Keterangan

Tidak XmlAttribute memiliki anak. Gunakan Value untuk menetapkan nilai teks ke atribut atau menggunakan AppendChild (atau metode serupa) untuk menambahkan anak ke atribut .

Berlaku untuk