XPathNavigator.CreateAttribute Method (String, String, String, String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Creates an attribute node on the current element node using the namespace prefix, local name and namespace URI specified with the value specified.

Namespace:   System.Xml.XPath
Assembly:  System.Xml (in System.Xml.dll)

public virtual void CreateAttribute(
	string prefix,
	string localName,
	string namespaceURI,
	string value


Type: System.String

The namespace prefix of the new attribute node (if any).

Type: System.String

The local name of the new attribute node which cannot String.Empty or null.

Type: System.String

The namespace URI for the new attribute node (if any).

Type: System.String

The value of the new attribute node. If String.Empty or null are passed, an empty attribute node is created.

Exception Condition

The XPathNavigator is not positioned on an element node.


The XPathNavigator does not support editing.

Namespace prefix and URI values can be obtained using the LookupPrefix or LookupNamespace method. For example, the following syntax creates an attribute by using the in-scope namespace xmlns:bk="http://www.contoso.com/books":

editor.CreateAttribute(navigator.Prefix, "attributeName", LookupNamespace(navigator.Prefix), "text")

This creates the new attribute <bk:element attributeName="text"/> on the current element.

The following are important notes to consider when using the CreateAttribute method.

  • If the namespace prefix specified is String.Empty or null, the prefix for the namespace URI of the new attribute is obtained from the current namespaces in-scope. If there is no namespace prefix assigned to the specified namespace URI at the current scope, a namespace prefix is automatically generated. For example to create a new attribute on an element in the default namespace of the contosoBooks.xml file, (xmlns="http://www.contoso.com/books"), you specify null or String.Empty for both the namespace prefix and namespace URI parameters. Specifying http://www.contoso.com/books as the namespace URI parameter will cause the CreateAttribute method to auto generate a namespace prefix for the new attribute.

  • If the new attribute created is a namespace node which conflicts with a namespace declaration on the element, either because the namespace prefix chosen is used by another namespace declaration at the same scope, or because the prefix chosen is the same as that of the element but is bound to a different namespace URI, an exception is thrown.

  • The CreateAttribute method does not affect the position of the XPathNavigator.

In the following example, a new discount attribute is created on the price child element of the first book element in the contosoBooks.xml file.

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

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

navigator.CreateAttribute("", "discount", "", "1.00");


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

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>

.NET Framework
Available since 2.0
Available since 4.0
Return to top