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 ファイル内の最初の XmlWriter 要素の CreateAttributes 子要素に作成されます。

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呼び出されるまで挿入されません。

  • 指定された名前空間プレフィックスが または nullの場合、String.Empty新しい属性の名前空間 URI のプレフィックスは、スコープ内の現在の名前空間から取得されます。 現在のスコープで指定された名前空間 URI に名前空間プレフィックスが割り当てられていない場合は、名前空間プレフィックスが自動的に生成されます。 たとえば、ファイルの既定の名前空間内の要素に新しい属性を作成するには(xmlns="http://www.contoso.com/books")、名前空間プレフィックスと名前空間 URI パラメーターのcontosoBooks.xml両方に または String.Empty を指定nullします。 名前空間 URI パラメーターとして を http://www.contoso.com/books 指定すると、メソッドは CreateAttribute 新しい属性の名前空間プレフィックスを自動生成します。

  • 選択した名前空間プレフィックスが同じスコープの別の名前空間宣言によって使用されているか、選択したプレフィックスが要素の同じであり、別の名前空間 URI にバインドされているために、作成された新しい属性が要素の名前空間宣言と競合する名前空間ノードである場合は、例外がスローされます。

  • 返されるオブジェクトは XmlWriter 、属性の作成にのみ使用できます。 属性を作成しない他 XmlWriter のオブジェクト メソッドを呼び出すと、例外がスローされます。

  • メソッドは CreateAttributes の位置には XPathNavigator影響しません。

適用対象