setAttribute Method

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

setAttribute Method

Sets the value of the named attribute.

[Script]

Script Syntax

oXMLDOMElement.setAttribute(name, value);

Parameters

name
The string specifying the name of the attribute. If the attribute with that name already exists, its value is changed. If the attribute with that name does not exist, it is created.
value
The variant that supplies the value for the named attribute.

Example

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var nodeBook;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   nodeBook = xmlDoc.selectSingleNode("//book");
   nodeBook.setAttribute("PublishDate", String(Date()))
   alert(nodeBook.getAttribute("PublishDate"));
}
[Visual Basic]

Visual Basic Syntax

oXMLDOMElement.setAttribute(name, value)

Parameters

name
The string specifying the name of the attribute. If the attribute with that name already exists, its value is changed. If the attribute with that name does not exist, it is created.
value
The variant that supplies the value for the named attribute.

Example

Dim xmlDoc As New Msxml2.DOMDocument50
Dim nodeBook As IXMLDOMElement
xmlDoc.async = False
xmlDoc.Load "books.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set nodeBook = xmlDoc.selectSingleNode("//book")
   nodeBook.setAttribute "PublishDate", Now
   MsgBox nodeBook.getAttribute("PublishDate")
End If
[C/C++]

C/C++ Syntax

HRESULT setAttribute(
    BSTR name,
    VARIANT value);

Parameters

name [in]
The name of the attribute. If an attribute with that name already exists, its value is changed. If an attribute with that name does not exist, it is created.
value [in]
The value for the named attribute.

C/C++ Return Values

S_OK
The value returned if successful.
E_FAIL
The value returned if an error occurs.

C/C++ Example

See the example in the getAttribute method.

Remarks

If an attribute with the supplied name already exists, this method changes its value to the supplied value parameter. The supplied string is not parsed, so any markup, such as syntax intended to be recognized as an entity reference, is treated as literal text and must be appropriately escaped by the implementation when it is written out.

To assign an attribute value that contains entity references, the user must create IXMLDOMAttribute plus any IXMLDOMText and IXMLDOMEntityReference objects, build the appropriate subtree, and call the setAttributeNode method.

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

IXMLDOMAttribute | IXMLDOMText | IXMLDOMEntityReference | setAttributeNode method

Applies to: IXMLDOMElement