IXMLDOMNode.replaceChild (C#)

banner art

Previous Next

IXMLDOMNode.replaceChild (C#)

The replaceChild method replaces the specified old child node with the supplied new child node.

Syntax

  IXMLDOMNode = IXMLDOMNode
  .replaceChild(
  object objnewChild,
 object objoldChild
);

Parameters

objnewChild

[in] object containing the address of the new child that is to replace the old child. If this parameter is NULL, objoldChild is removed without a replacement.

objoldChild

[in] object containing the address of the old child that is to be replaced by the new child.

Return Values

When getting the value, this method returns the old child node that is replaced by the new child node.

Remarks

When replacing an old child node with a new child node, the new child must be a valid child type for the existing parent node. The following tables list which child node types are valid and not valid for the parent node types specified.

The following table lists possible child node types for the parent node type NODE_ATTRIBUTE.

Child node type Description
NODE_ATTRIBUTE
NODE_COMMENT
NODE_ELEMENT
Not valid. Returns an error. These node types cannot be children of an attribute.
NODE_PROCESSING_INSTRUCTION Not valid. Returns an error. This node type either cannot be a child of an attribute node or it is read-only.
NODE_TEXT Replaces the specified objoldChild with the supplied objnewChild and returns objoldChild.

The following table lists possible child node types for the parent node type NODE_DOCUMENT.

Child node type Description
NODE_ATTRIBUTE
NODE_DOCUMENT
NODE_TEXT
Not valid. Returns an error. These nodes cannot be children of a document node.
NODE_COMMENT
NODE_PROCESSING_INSTRUCTION
Replaces the specified objoldChild with the supplied objnewChild and returns objoldChild.
NODE_ELEMENT Replaces objoldChild with objnewChild and returns objoldChild. By definition, an XML document (the document node) can have only a single child element. Therefore, an error is returned if the document node already has a child element.

The following table lists possible child node types for the parent node type NODE_ELEMENT.

Child node type Description
NODE_ATTRIBUTE
NODE_DOCUMENT
Not valid. Returns an error. These node types cannot be children of an element node.
NODE_COMMENT
NODE_ELEMENT
NODE_PROCESSING_INSTRUCTION
NODE_TEXT
Replaces the specified objoldChild with objnewChild and returns objoldChild.

Example Code

The following example creates a new IXMLDOMNode object, newElem, and replaces the specified child node with a new node.

using Microsoft.WindowsMediaServices.Interop;
using interop_msxml;

// Declare variables.
WMSServer Server;
IXMLDOMDocument Playlist;
IXMLDOMElement Root;
IXMLDOMElement newElem;

try {
    // Create a new WMSServer object.
    Server = new WMSServerClass();

    // Create a new playlist object.
    Playlist = Server.CreatePlaylist();

    // Load a playlist.
    Playlist.load("file://c:\\wmpub\\wmroot\\simple.wsx");

    // Retrieve the root element.
    Root = Playlist.documentElement;

    // Create a new element.
    newElem = Playlist.createElement("seq");

    // Replace the child of the current node with
    // the newly created node.
    Root.childNodes[1].replaceChild(
        newElem, root.childNodes[1].childNodes[0]);
}
catch (Exception e) {
    // TODO: Handle exceptions.
}

Requirements

Reference: Add references to Microsoft.WindowsMediaServices and interop_msxml.

Namespace: Microsoft.WindowsMediaServices.Interop, interop_msxml.

Assembly: Microsoft.WindowsMediaServices.dll, interop_msxml.dll.

Library: WMSServerTypeLib.dll, msxml.dll.

Platform: Windows Server 2003 family, Windows Server 2008 family.

See Also

Previous Next