XPathNavigator.CreateAttributes 方法

定義

回傳 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子元素上建立新的XmlWriterCreateAttributes屬性。

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 的新屬性會放在目前元素屬性清單的末尾。

  • Close 物件的方法 XmlWriter 被呼叫之前,這些新屬性不會入。

  • 若指定的命名空間前綴為或String.Emptynull,則新屬性的命名空間 URI 前綴可從目前範圍內的命名空間取得。 如果目前範圍內指定的命名空間 URI 沒有指定命名空間前綴,則會自動產生命名空間前綴。 例如,要在檔案預設命名空間 contosoBooks.xml 的元素()xmlns="http://www.contoso.com/books"上建立新屬性,你就指定 null or String.Empty 來指定命名空間前綴和命名空間 URI 參數。 指定 http://www.contoso.com/books 為命名空間的 URI 參數,會使 CreateAttribute 方法自動產生新屬性的命名空間前綴。

  • 如果新建立的屬性是與元素命名空間宣告衝突的命名空間節點,無論是因為所選命名空間前綴被同一範圍內的另一個命名空間宣告使用,或所選前綴與元素相同但綁定於不同的命名空間 URI,則會拋出例外。

  • 回傳的 XmlWriter 物件只能用來建立屬性。 呼叫 XmlWriter 其他不產生屬性的物件方法會拋出例外。

  • CreateAttributes 方法不影響 的位置 XPathNavigator

適用於