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 不是位於項目節點上。

範例

在下列範例中,會使用從 discount 方法傳回的 currency 物件,在 price 檔案中第一個 book 項目的 contosoBooks.xml 項目子系上,建立新的 XmlWriterCreateAttributes 屬性。

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

XmlWriter^ attributes = navigator->CreateAttributes();

attributes->WriteAttributeString("discount", "1.00");
attributes->WriteAttributeString("currency", "USD");
attributes->Close();

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

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

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

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

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

  • XmlWriter傳回的物件只能用來建立屬性。 呼叫未建立屬性的其他 XmlWriter 物件方法會擲回例外狀況。

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

適用於