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

不能為 Emptynull 的新屬性節點的區域名稱。

namespaceURI
String

新屬性節點的命名空間 URI (如果有的話)。

value
String

新屬性節點的值。 如果已傳遞 Emptynull,則會建立空的屬性節點。

例外狀況

XPathNavigator 不是位於項目節點上。

範例

在下列範例中,會在檔案中第一 bookcontosoBooks.xml 個專案的子項目上 price 建立新的 discount 屬性。

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 檔案做為輸入。

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

備註

您可以使用 或 LookupNamespace 方法來取得 LookupPrefix 命名空間前置詞和 URI 值。 例如,下列語法會使用範圍內的命名空間 xmlns:bk="http://www.contoso.com/books" 來建立屬性:

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

這會在目前專案上建立新的屬性 <bk:element attributeName="text"/>

以下是使用 CreateAttribute 方法時要考慮的重要注意事項。

  • 如果指定的命名空間前置詞為 String.Emptynull ,則新屬性之命名空間 URI 的前置詞會從目前範圍內的命名空間取得。 如果目前範圍沒有指派給指定命名空間 URI 的命名空間前置詞,則會自動產生命名空間前置詞。 例如,若要在檔案的預設命名空間 contosoBooks.xml 中建立新的屬性, () xmlns="http://www.contoso.com/books" ,您可以同時指定 nullString.Empty 用於命名空間前置詞和命名空間 URI 參數。 指定 http://www.contoso.com/books 為命名空間 URI 參數會導致 CreateAttribute 方法自動產生新屬性的命名空間前置詞。

  • 如果建立的新屬性是命名空間節點,與元素上的命名空間宣告發生衝突,可能是因為所選命名空間前置詞是由相同範圍的另一個命名空間宣告使用,或因為所選的前置詞與專案相同,但系結至不同的命名空間 URI,所以會擲回例外狀況。

  • 方法 CreateAttribute 不會影響 的位置 XPathNavigator

適用於