XPathNavigator.CreateAttribute(String, String, String, String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在當前元素節點上建立屬性節點,使用命名空間前綴、本地名稱及指定命名空間 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
新屬性節點(若有)的命名空間前綴。
- namespaceURI
- String
新屬性節點的命名空間 URI(如果有的話)。
例外狀況
該節點 XPathNavigator 不存在於元素節點上。
它 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>
備註
命名空間前綴與 URI 值可透過 LookupPrefix or LookupNamespace 方法取得。 例如,以下語法透過使用範圍內的命名空間 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"上建立新屬性,你就指定nullor String.Empty 來指定命名空間前綴和命名空間 URI 參數。 指定http://www.contoso.com/books為命名空間的 URI 參數,會使 CreateAttribute 方法自動產生新屬性的命名空間前綴。如果新建立的屬性是與元素命名空間宣告衝突的命名空間節點,無論是因為所選命名空間前綴被同一範圍內的另一個命名空間宣告使用,或所選前綴與元素相同但綁定於不同的命名空間 URI,則會拋出例外。
此 CreateAttribute 方法不影響 的位置 XPathNavigator。