setAttributeNode Method

MSXML 5.0 SDK

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

setAttributeNode Method

Sets or updates the supplied attribute node on this element.

[Script]

Script Syntax

var objXMLDOMAttribute = oXMLDOMElement.XMLDOMElement(DOMAttribute);

Parameters

DOMAttribute
An object that contains the attribute node to be associated with this element.

Return Value

An object. Returns Null unless the new attribute replaces an existing attribute with the same name, in which case this method returns the previous, replaced attribute node.

Example

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

Visual Basic Syntax

Set objXMLDOMAttribute = oXMLDOMElement.XMLDOMElement(DOMAttribute)

Parameters

DOMAttribute
An object that contains the attribute node to be associated with this element.

Return Value

An object. Returns Null unless the new attribute replaces an existing attribute with the same name, in which case this method returns the previous, replaced attribute node.

Example

Dim xmlDoc As New Msxml2.DOMDocument50
Dim nodeBook As IXMLDOMElement
Dim nodePublishDate As IXMLDOMAttribute
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 nodePublishDate = xmlDoc.createAttribute("PublishDate")
   nodePublishDate.Value = Now
   Set nodeBook = xmlDoc.selectSingleNode("//book")
   nodeBook.setAttributeNode nodePublishDate
   MsgBox nodeBook.getAttribute("PublishDate")
End If
[C/C++]

C/C++ Syntax

HRESULT setAttributeNode(
    IXMLDOMAttribute *DOMAttribute,
    IXMLDOMAttribute **attributeNode);

Parameters

DOMAttribute [in]
An attribute node that is to be associated with this element.
attributeNode [out, retval]
Null unless the new attribute replaces an existing attribute with the same name, in which case this method returns the previous, replaced attribute node.

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 getAttributeNode method.

Remarks

You cannot add an existing attribute to an element until you first remove it from its previous element. Also, you cannot add a namespace-qualified attribute when it uses the same prefix as another attribute with a different namespaceURI.

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

namespaceURI Property

Applies to: IXMLDOMElement