Click to Rate and Give Feedback
MSDN
MSDN Library
MSXML
DOM
DOM Reference
XML DOM Methods
 setAttribute Method
setAttribute Method

Sets the value of the named attribute.

JScript 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

NoteNote

You can use books.xml to run this sample code.

J#
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var nodeBook;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   WScript.Echo("You have error " + myErr.reason);
} else {
   nodeBook = xmlDoc.selectSingleNode("//book");
   nodeBook.setAttribute("PublishDate", String(Date()))
   WScript.Echo(nodeBook.getAttribute("PublishDate"));
}

Output

Outputs the current date and time in the following format:

Thu Jun 12 14:12:38 2003

(Thursday, June 12, 2003, 2:12:38 pm)

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

NoteNote

You can use books.xml to run this sample code.

Visual Basic
Dim xmlDoc As New Msxml2.DOMDocument30
Dim nodeBook As IXMLDOMElement
xmlDoc.async = False
xmlDoc.Load App.Path & "\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

Output

Outputs the current date and time in the following format:

6/12/2003 2:12:38 PM

(June 12, 2003, 2:12:38 PM)

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.

Return Values

S_OK

The value returned if successful.

E_FAIL

The value returned if an error occurs.

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.

Versioning

MSXML 3.0 and later

See Also

© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker