This documentation is archived and is not being maintained.

XmlElement.SetAttribute Method

Sets the value of the specified attribute.

Overload List

Sets the value of the attribute with the specified name.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub SetAttribute(String, String)
[C#] public virtual void SetAttribute(string, string);
[C++] public: virtual void SetAttribute(String*, String*);
[JScript] public function SetAttribute(String, String);

Sets the value of the attribute with the specified local name and namespace URI.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function SetAttribute(String, String, String) As String
[C#] public virtual string SetAttribute(string, string, string);
[C++] public: virtual String* SetAttribute(String*, String*, String*);
[JScript] public function SetAttribute(String, String, String) : String;

Example

[Visual Basic, C#, C++] The following example adds an attribute to an element.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of SetAttribute. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
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.
    root.SetAttribute("genre", "urn:samples", "novel")

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

  end sub
end class

[C#] 
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.
    root.SetAttribute("genre", "urn:samples", "novel");

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

  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<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.
    root->SetAttribute(S"genre", S"urn:samples", S"novel");

    Console::WriteLine(S"Display the modified XML...");
    Console::WriteLine(doc->InnerXml);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

XmlElement Class | XmlElement Members | System.Xml Namespace

Show: