Export (0) Print
Expand All

XMLNodes.Add Method

Gets a Microsoft.Office.Interop.Word.XMLNode object that represents a newly added element.

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word (in Microsoft.Office.Tools.Word.dll)

XMLNode Add(
	string Name,
	string Namespace,
	ref Object Range
)

Parameters

Name
Type: System.String

The name of the element in the XML schema designated in the Namespace parameter. Because XML is case-sensitive, the spelling of the element specified in the Name parameter must be exactly as it appears in the schema. If it does not match any of the element names in the schema specified in the Namespace parameter, an error is displayed.

Namespace
Type: System.String

The name of the schema as defined in the schema. The Namespace parameter is case-sensitive and must be spelled exactly as it appears in the schema. If the specified namespace cannot be found in any of the schemas attached to the document, an error is displayed.

Range
Type: System.Object

The range to which you want to apply the element. The default is to place the element tags at the insertion point or around the selection if a text is selected.

Return Value

Type: Microsoft.Office.Interop.Word.XMLNode
A Microsoft.Office.Interop.Word.XMLNode object that represents a newly added element.

Optional Parameters

For information on optional parameters, see Optional Parameters in Office Solutions.

The following code example demonstrates event handlers for the AfterInsert and BeforeDelete events. The code displays a message box before a Microsoft.Office.Interop.Word.XMLNode is deleted from the document and after a Microsoft.Office.Interop.Word.XMLNode is added to the document. The example also uses the Add method to add a new element to an XMLNodes collection at a specified range and programmatically raise the AfterInsert event. This example assumes that the current document contains an XMLNodes collection named SampleInsertNodes, which has a parent XMLNode named SampleNode.

private void XMLNodeAddAndDelete()
{
    object range1 = this.SampleInsertNodes[1].Range;

    this.SampleInsertNodes.AfterInsert +=
        new Microsoft.Office.Tools.Word.NodeInsertAndDeleteEventHandler(
        SampleInsertNodes_AfterInsert);

    this.SampleInsertNodes.BeforeDelete +=
        new Microsoft.Office.Tools.Word.NodeInsertAndDeleteEventHandler(
        SampleInsertNodes_BeforeDelete);

    // This will raise the AfterInsert event.
    Word.XMLNode node1 = this.SampleInsertNodes.Add("MemoFrom",
        this.SampleNode.NamespaceURI, ref range1);
}

void SampleInsertNodes_AfterInsert(object sender,
    Microsoft.Office.Tools.Word.NodeInsertAndDeleteEventArgs e)
{
    if (e.InUndoRedo)
    {
        MessageBox.Show("A child element of SampleInsertNodes was " +
            "inserted as a result of an undo or redo operation.");
    }
    else
    {
        MessageBox.Show("A child element of SampleInsertNodes " +
            "was inserted.");
    }
}

void SampleInsertNodes_BeforeDelete(object sender,
    Microsoft.Office.Tools.Word.NodeInsertAndDeleteEventArgs e)
{
    if (e.InUndoRedo)
    {
        MessageBox.Show("A child element of SampleInsertNodes " +
            "is about to be deleted as a result of an undo or " +
            "redo operation.");
    }
    else
    {
        MessageBox.Show("A child element of SampleInsertNodes " +
            "is about to be deleted.");
    }
}

Show:
© 2014 Microsoft