XPathNavigator.CreateAttributes 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳 XmlWriter 一個用於在當前元素上建立新屬性的物件。
public:
virtual System::Xml::XmlWriter ^ CreateAttributes();
public virtual System.Xml.XmlWriter CreateAttributes();
abstract member CreateAttributes : unit -> System.Xml.XmlWriter
override this.CreateAttributes : unit -> System.Xml.XmlWriter
Public Overridable Function CreateAttributes () As XmlWriter
傳回
一個 XmlWriter 用來在當前元素上建立新屬性的物件。
例外狀況
該節點 XPathNavigator 不存在於元素節點上。
它 XPathNavigator 不支援編輯。
範例
在下列範例中,會使用從 discount 方法傳回的currency物件,在price檔案中第一個book元素的contosoBooks.xml子元素上建立新的XmlWriter和CreateAttributes屬性。
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");
XmlWriter attributes = navigator.CreateAttributes();
attributes.WriteAttributeString("discount", "1.00");
attributes.WriteAttributeString("currency", "USD");
attributes.Close();
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")
Dim attributes As XmlWriter = navigator.CreateAttributes()
attributes.WriteAttributeString("discount", "1.00")
attributes.WriteAttributeString("currency", "USD")
attributes.Close()
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>
備註
以下是使用此 CreateAttributes 方法時需注意的重要事項。
當 被 XPathNavigator 放置在元素上時,該方法建立 XPathNavigator 的新屬性會放在目前元素屬性清單的末尾。
若指定的命名空間前綴為或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,則會拋出例外。
此 CreateAttributes 方法不影響 的位置 XPathNavigator。