XmlElement.SetAttributeNode Metod

Definition

Lägger till en ny XmlAttribute.

Överlagringar

Name Description
SetAttributeNode(XmlAttribute)

Lägger till den angivna XmlAttribute.

SetAttributeNode(String, String)

Lägger till den angivna XmlAttribute.

SetAttributeNode(XmlAttribute)

Källa:
XmlElement.cs
Källa:
XmlElement.cs
Källa:
XmlElement.cs
Källa:
XmlElement.cs
Källa:
XmlElement.cs

Lägger till den angivna XmlAttribute.

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

Parametrar

newAttr
XmlAttribute

Noden XmlAttribute som ska läggas till i attributsamlingen för det här elementet.

Returer

Om attributet ersätter ett befintligt attribut med samma namn returneras det gamla XmlAttribute . Annars null returneras det.

Undantag

Skapades newAttr från ett annat dokument än det som skapade den här noden. Eller så är den här noden skrivskyddad.

newAttr är redan ett attribut för ett annat XmlElement objekt. Du måste uttryckligen klona XmlAttribute noder för att återanvända dem i andra XmlElement objekt.

Kommentarer

Om ett attribut med det namnet redan finns i elementet ersätts det med det nya.

Gäller för

SetAttributeNode(String, String)

Källa:
XmlElement.cs
Källa:
XmlElement.cs
Källa:
XmlElement.cs
Källa:
XmlElement.cs
Källa:
XmlElement.cs

Lägger till den angivna XmlAttribute.

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

Parametrar

localName
String

Attributets lokala namn.

namespaceURI
String

Attributets namnområdes-URI.

Returer

Att XmlAttribute lägga till.

Exempel

I följande exempel läggs ett attribut till i ett element.

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

Kommentarer

De XmlAttribute har inga barn. Använd Value för att tilldela ett textvärde till attributet eller använd AppendChild (eller en liknande metod) för att lägga till underordnade i attributet.

Gäller för