XPathNavigator.CreateAttributes メソッド
アセンブリ: System.Xml (system.xml.dll 内)

Dim instance As XPathNavigator Dim returnValue As XmlWriter returnValue = instance.CreateAttributes
現在の要素に新しい属性を作成するために使用する XmlWriter オブジェクト。


CreateAttributes メソッドを使用するときに考慮する必要がある重要な注意事項を次に示します。
-
XPathNavigator が要素に配置されている場合、XPathNavigator メソッドで作成される新しい属性は、現在の要素の属性リストの末尾に配置されます。
-
指定された名前空間プレフィックスが String.Empty または null 参照 (Visual Basic では Nothing) の場合、新しい属性の名前空間 URI 用のプレフィックスは、現在の名前空間のスコープ内から取得されます。指定した名前空間 URI に割り当てられた名前空間プレフィックスが現在のスコープにない場合、名前空間プレフィックスが自動的に生成されます。たとえば、contosoBooks.xml ファイルの既定の名前空間 (xmlns="http://www.contoso.com/books") の要素に新しい属性を作成するために、名前空間プレフィックス パラメータと名前空間 URI パラメータの両方に、null 参照 (Visual Basic では Nothing) または String.Empty を指定するとします。名前空間 URI パラメータとして http://www.contoso.com/books を指定すると、CreateAttribute メソッドによって新しい属性の名前空間プレフィックスが自動的に生成されます。
-
選択された名前空間プレフィックスが同じスコープで別の名前空間宣言によって使用されているか、または、要素のプレフィックスと同じプレフィックスが選択されたが異なる名前空間 URI に関連付けられているために、作成された新しい属性が、要素の名前空間宣言と競合する名前空間ノードとなった場合、例外がスローされます。
-
返される XmlWriter オブジェクトは、属性を作成するためにだけ使用できます。属性を作成しない他の XmlWriter オブジェクト メソッドを呼び出すと、例外がスローされます。

CreateAttributes メソッドから返される XmlWriter オブジェクトを使用して、contosoBooks.xml ファイル内にある最初の book 要素の price 子要素に、新しい discount 属性と currency 属性を作成する例を次に示します。
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)
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);
XPathDocument^ document = gcnew XPathDocument("contosoBooks.xml"); XPathNavigator^ navigator = document->CreateNavigator(); navigator->MoveToChild("bookstore", "http://www.contoso.com/books"); navigator->MoveToChild("book", "http://www.contoso.com/books"); // Select all the descendant nodes of the book node and // display the LocalName of each descendant node. Console::WriteLine("Descendant nodes of the book node:"); for each(XPathNavigator^ book in navigator-> SelectDescendants("", "http://www.contoso.com/books", false)) { Console::WriteLine(book->Name); } // Select all the child nodes of the book node and // display the LocalName of each child node. Console::WriteLine("\nChild nodes of the book node:"); for each(XPathNavigator^ book in navigator-> SelectChildren("", "http://www.contoso.com/books")) { Console::WriteLine(book->Name); } // Select all the ancestor nodes of the title node. navigator->MoveToChild("title", "http://www.contoso.com/books"); XPathNodeIterator^ bookAncestors = navigator->SelectAncestors("", "http://www.contoso.com/books", false); // Display the LocalName of each ancestor node. Console::WriteLine("\nAncestor nodes of the title node:"); while(bookAncestors->MoveNext()) { Console::WriteLine(bookAncestors->Current->Name); }
この例では、入力として、contosoBooks.xml というファイルを使用しています。
<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>

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からXPathNavigator.CreateAttributes メソッドを検索する場合は、下記のリンクをクリックしてください。

- XPathNavigator.CreateAttributes メソッドのページへのリンク