语言

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

定义

使用指定值的命名空间前缀、本地名称和命名空间 URI 在当前元素节点上创建属性节点。

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);
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

新属性节点的命名空间前缀(如果有)。

localName
String

新属性节点的本地名称,不能 Empty 或 null。

namespaceURI
String

新属性节点的命名空间 URI(如果有)。

value
String

新属性节点的值。 如果 Empty 已传递或 null 传递,则会创建一个空的属性节点。

例外

该 XPathNavigator 元素未定位在元素节点上。

示例

在以下示例中,在文件中第一discount个元素的子元素price上book创建了一个新contosoBooks.xml属性。

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 文件作为输入。

<?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>

注解

可以使用或LookupPrefix方法获取LookupNamespace命名空间前缀和 URI 值。 例如,以下语法使用作用域内命名空间 xmlns:bk="http://www.contoso.com/books"创建属性:

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

这会在当前元素上创建新属性 <bk:element attributeName="text"/> 。

以下是使用 CreateAttribute 该方法时要考虑的重要说明。

  • 如果指定的命名空间前缀为 String.Empty 或 null,则从当前范围内的命名空间获取新属性的命名空间 URI 的前缀。 如果当前范围内没有分配给指定命名空间 URI 的命名空间前缀,则自动生成命名空间前缀。 例如,若要在文件的默认命名空间contosoBooks.xml中的元素上创建新属性(xmlns="http://www.contoso.com/books"),请指定或null同时指定String.Empty命名空间前缀和命名空间 URI 参数。 指定 http://www.contoso.com/books 为命名空间 URI 参数将导致 CreateAttribute 该方法自动生成新属性的命名空间前缀。

  • 如果创建的新属性是与元素上的命名空间声明冲突的命名空间节点,要么是因为选择的命名空间前缀由同一作用域的另一个命名空间声明使用,或者因为所选前缀与元素的前缀相同,但绑定到其他命名空间 URI,则会引发异常。

  • 该方法 CreateAttribute 不会影响该 XPathNavigator方法的位置。

适用于