XPathNavigator.CreateAttribute(String, String, String, String) 方法

定义

使用通过指定值指定的命名空间前缀、本地名称和命名空间 URI 在当前元素节点上创建一个属性节点。Creates an attribute node on the current element node using the namespace prefix, local name and namespace URI specified with the value specified.

public:
 virtual void CreateAttribute(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI, System::String ^ value);
public virtual void CreateAttribute (string prefix, string localName, string namespaceURI, string value);
abstract member CreateAttribute : string * string * string * string -> unit
override this.CreateAttribute : string * string * string * string -> unit
Public Overridable Sub CreateAttribute (prefix As String, localName As String, namespaceURI As String, value As String)

参数

prefix
String

新的属性节点的命名空间前缀(如果有)。The namespace prefix of the new attribute node (if any).

localName
String

新的属性节点的本地名称,该名称不能是 EmptynullThe local name of the new attribute node which cannot Empty or null.

namespaceURI
String

新的属性节点的命名空间 URI(如果有)。The namespace URI for the new attribute node (if any).

value
String

新的属性节点的值。The value of the new attribute node. 如果传递了 Emptynull,则将创建一个空属性节点。If Empty or null are passed, an empty attribute node is created.

例外

XPathNavigator 未定位在元素节点上。The XPathNavigator is not positioned on an element node.

XPathNavigator 不支持编辑。The XPathNavigator does not support editing.

示例

在下面的示例中,在 discount price 文件中第一个元素的子元素上创建一个新属性 book contosoBooks.xmlIn 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 = gcnew XmlDocument();
document->Load("contosoBooks.xml");
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");

navigator->MoveToParent();
Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
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");

navigator.MoveToParent();
Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = 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")

navigator.MoveToParent()
Console.WriteLine(navigator.OuterXml)

该示例使用 contosoBooks.xml 文件作为输入。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>  
        <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>  

注解

可以使用或方法获取命名空间前缀和 URI LookupPrefixLookupNamespaceNamespace prefix and URI values can be obtained using the LookupPrefix or LookupNamespace method. 例如,下面的语法通过使用范围内命名空间来创建属性 xmlns:bk="http://www.contoso.com/books"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")  

这将 <bk:element attributeName="text"/> 在当前元素上创建新属性。This creates the new attribute <bk:element attributeName="text"/> on the current element.

以下是使用方法时要考虑的重要注意事项 CreateAttributeThe following are important notes to consider when using the CreateAttribute method.

  • 如果指定的命名空间前缀为 String.Empty 或,则将 null 从作用域内的当前命名空间获取新属性的命名空间 URI 的前缀。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. 如果当前范围内未给指定的命名空间 URI 分配命名空间前缀,则会自动生成一个命名空间前缀。If there is no namespace prefix assigned to the specified namespace URI at the current scope, a namespace prefix is automatically generated. 例如,若要在文件的默认命名空间中的元素上创建新属性 contosoBooks.xml ( (xmlns="http://www.contoso.com/books") ,请 null String.Empty 为命名空间前缀和命名空间 URI 参数指定或。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. 指定 http://www.contoso.com/books 作为命名空间 URI 参数将导致 CreateAttribute 方法自动为新属性生成命名空间前缀。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.

  • 如果创建的新属性是一个命名空间节点,该节点与元素上的命名空间声明冲突,原因是所选的命名空间前缀是由同一范围内的另一个命名空间声明使用的,或者因为所选前缀与元素的名称相同,但绑定到不同的命名空间 URI,则会引发异常。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.

  • CreateAttribute方法不影响的位置 XPathNavigatorThe CreateAttribute method does not affect the position of the XPathNavigator.

适用于