System.Xml.XPath.XPathNavigator.CreateAttribute Method

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

Syntax

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

Parameters

prefix
The namespace prefix of the new attribute node (if any).
localName
The local name of the new attribute node which cannot string.Empty or null.
namespaceURI
The namespace URI for the new attribute node (if any).
value
The value of the new attribute node. If string.Empty or null are passed, an empty attribute node is created.

Remarks

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

Example

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 XPathNavigator.CreateAttribute(string, string, string, string) 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 XPathNavigator.CreateAttribute(string, string, string, string) 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 XPathNavigator.CreateAttribute(string, string, string, string) method does not affect the position of the System.Xml.XPath.XPathNavigator.

Requirements

Namespace: System.Xml.XPath
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0