Share via


Remove XML Data using XPathNavigator

The XPathNavigator class provides a set of methods used to remove nodes and values from an XML document. In order to use these methods, the XPathNavigator object must be editable, that is, its CanEdit property must be true.

XPathNavigator objects that can edit an XML document are created by the CreateNavigator method of the XmlDocument class. XPathNavigator objects created by the XPathDocument class are read-only and any attempt to use the editing methods of an XPathNavigator object created by an XPathDocument object results in a NotSupportedException.

For more information about creating editable XPathNavigator objects, see Reading XML Data using XPathDocument and XmlDocument.

Removing Nodes

The XPathNavigator class provides the DeleteSelf method to remove nodes from an XML document.

Removing a Node

The XPathNavigator class provides the DeleteSelf method to delete the current node an XPathNavigator object is currently positioned on from an XML document.

After a node has been deleted using the DeleteSelf method, it is no longer reachable from the root of the XmlDocument object. After a node has been deleted, the XPathNavigator is positioned on the parent node of the deleted node.

A delete operation doesn't affect the position of any XPathNavigator object positioned on the deleted node. These XPathNavigator objects are valid in the sense that they can move within the deleted subtree, but cannot be moved to the main node tree using the regular node set navigation methods of the XPathNavigator class.

Note

The MoveTo method of the XPathNavigator class can be used to move these XPathNavigator objects back into the main node tree, or from the main node tree to the deleted subtree.

In the following example, the price element of the first book element of the contosoBooks.xml file is deleted using the DeleteSelf method. The position of the XPathNavigator object after the price element is deleted is on the parent book element.

Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "https://www.contoso.com/books")
navigator.MoveToChild("book", "https://www.contoso.com/books")
navigator.MoveToChild("price", "https://www.contoso.com/books")

navigator.DeleteSelf()

Console.WriteLine("Position after delete: {0}", navigator.Name)
Console.WriteLine(navigator.OuterXml)
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "https://www.contoso.com/books");
navigator.MoveToChild("book", "https://www.contoso.com/books");
navigator.MoveToChild("price", "https://www.contoso.com/books");

navigator.DeleteSelf();

Console.WriteLine("Position after delete: {0}", navigator.Name);
Console.WriteLine(navigator.OuterXml);

The example takes the contosoBooks.xml file as an input.

<bookstore xmlns="https://www.contoso.com/books">
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

Removing an Attribute Node

Attribute nodes are removed from an XML document using the DeleteSelf method.

After an attribute node has been deleted, it is no longer reachable from the root node of an XmlDocument object and the XPathNavigator object is positioned on the parent element.

Default Attributes

Regardless of the method used to remove attributes, there are special limitations on removing attributes that are defined as default attributes in the DTD or XML Schema for the XML document. Default attributes cannot be removed unless the element they belong to is also removed. Default attributes are always present for elements that have default attributes declared, and as a result, deleting a default attribute results in a replacement attribute being inserted into the element and initialized to the default value that was declared.

Removing Values

The XPathNavigator class provides the SetValue and SetTypedValue methods to remove untyped and typed values from an XML document.

Removing Untyped Values

The SetValue method simply inserts the untyped string value passed as a parameter as the value of the node the XPathNavigator object is currently positioned on. Passing an empty string to the SetValue method removes the value of the current node.

The following example removes the value of the price element of the first book element in the contosoBooks.xml file using the SetValue method.

Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "https://www.contoso.com/books")
navigator.MoveToChild("book", "https://www.contoso.com/books")
navigator.MoveToChild("price", "https://www.contoso.com/books")

navigator.SetValue("")

navigator.MoveToRoot()
Console.WriteLine(navigator.OuterXml)
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "https://www.contoso.com/books");
navigator.MoveToChild("book", "https://www.contoso.com/books");
navigator.MoveToChild("price", "https://www.contoso.com/books");

navigator.SetValue("");

navigator.MoveToRoot();
Console.WriteLine(navigator.OuterXml);

The example takes the contosoBooks.xml file as an input.

<bookstore xmlns="https://www.contoso.com/books">
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

Removing Typed Values

When the type of a node is a W3C XML Schema simple type, the new value inserted by the SetTypedValue method is checked against the facets of the simple type before the value is set. If the new value is not valid according to the type of the node (for example, setting a value of -1 on an element whose type is xs:positiveInteger), it results in an exception. The SetTypedValue method also cannot be passed null as a parameter. As a result removing the value of a typed node must comply with the schema type of the node.

The following example removes the value of the price element of the first book element in the contosoBooks.xml file using the SetTypedValue method by setting the value to 0. The value of the node is not removed, but the price of the book has been removed according to its data type of xs:decimal.

Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("https://www.contoso.com/books", "contosoBooks.xsd")
settings.ValidationType = ValidationType.Schema

Dim reader As XmlReader = XmlReader.Create("contosoBooks.xml", settings)

Dim document As XmlDocument = New XmlDocument()
document.Load(reader)
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "https://www.contoso.com/books")
navigator.MoveToChild("book", "https://www.contoso.com/books")
navigator.MoveToChild("price", "https://www.contoso.com/books")

navigator.SetTypedValue(0)

navigator.MoveToRoot()
Console.WriteLine(navigator.OuterXml)
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("https://www.contoso.com/books", "contosoBooks.xsd");
settings.ValidationType = ValidationType.Schema;

XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);

XmlDocument document = new XmlDocument();
document.Load(reader);
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "https://www.contoso.com/books");
navigator.MoveToChild("book", "https://www.contoso.com/books");
navigator.MoveToChild("price", "https://www.contoso.com/books");

navigator.SetTypedValue(0);

navigator.MoveToRoot();
Console.WriteLine(navigator.OuterXml);

Namespace Nodes

Namespace nodes cannot be deleted from an XmlDocument object. Attempts to delete namespace nodes using the DeleteSelf method results in an exception.

The InnerXml and OuterXml Properties

The InnerXml and OuterXml properties of the XPathNavigator class change the XML markup of the nodes an XPathNavigator object is currently positioned on.

The InnerXml property changes the XML markup of the child nodes an XPathNavigator object is currently positioned on with the parsed contents of the given XML string. Similarly, the OuterXml property changes the XML markup of the child nodes an XPathNavigator object is currently positioned on as well as the current node itself.

In addition to the methods described in this topic, the InnerXml and OuterXml properties can be used to remove nodes and values from an XML document. For more information about using the InnerXml and OuterXml properties to modify nodes, see the Modify XML Data using XPathNavigator topic.

Saving an XML Document

Saving changes made to an XmlDocument object as the result of the methods described in this topic is performed using the methods of the XmlDocument class. For more information about saving changes made to an XmlDocument object, see Saving and Writing a Document.

See Also

Concepts

Process XML Data Using the XPath Data Model

Insert XML Data using XPathNavigator

Modify XML Data using XPathNavigator

Reference

XmlDocument

XPathDocument

XPathNavigator