XPathNavigator.InsertElementBefore(String, String, String, String) メソッド

定義

値を指定して、指定された名前空間プレフィックス、ローカル名、および名前空間 URI を使用して、現在のノードの前に新しい兄弟要素を作成します。

public:
 virtual void InsertElementBefore(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI, System::String ^ value);
public virtual void InsertElementBefore(string? prefix, string localName, string? namespaceURI, string? value);
public virtual void InsertElementBefore(string prefix, string localName, string namespaceURI, string value);
abstract member InsertElementBefore : string * string * string * string -> unit
override this.InsertElementBefore : string * string * string * string -> unit
Public Overridable Sub InsertElementBefore (prefix As String, localName As String, namespaceURI As String, value As String)

パラメーター

prefix
String

新しい子要素の名前空間プレフィックス (存在する場合)。

localName
String

新しい子要素のローカル名 (存在する場合)。

namespaceURI
String

新しい子要素の名前空間 URI (存在する場合)。 Emptynull は同等です。

value
String

新しい子要素の値。 Emptyまたはnullが渡されると、空の要素が作成されます。

例外

XPathNavigatorの位置では、現在のノードの前に新しい兄弟ノードを挿入できません。

XPathNavigatorは編集をサポートしていません。

次の例では、pages ファイルの最初のprice要素のbook子要素の前に新しいcontosoBooks.xml要素が挿入されます。

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

navigator.InsertElementBefore(navigator.Prefix, "pages", navigator.LookupNamespace(navigator.Prefix), "100");

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

navigator.InsertElementBefore(navigator.Prefix, "pages", navigator.LookupNamespace(navigator.Prefix), "100")

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>

注釈

名前空間のプレフィックスと URI の値は、 LookupPrefix または LookupNamespace メソッドを使用して取得できます。 たとえば、次の構文では、スコープ内の名前空間 xmlns:bk="http://www.contoso.com/books"を使用して兄弟要素を挿入します。

navigator.InsertElementBefore(navigator.Prefix, "pages", LookupNamespaceURI(navigator.Prefix), String.Empty)

これにより、新しい兄弟 <bk:pages/> 要素が作成されます。

InsertElementBeforeメソッドを使用する際に考慮すべき重要な注意事項を次に示します。

  • 指定された名前空間プレフィックスが null または String.Emptyの場合、新しい要素の名前空間 URI のプレフィックスは、スコープ内の現在の名前空間から取得されます。 現在のスコープで指定した名前空間 URI に名前空間プレフィックスが割り当てられていない場合は、名前空間プレフィックスが自動的に生成されます。

  • InsertElementBefore メソッドは、XPathNavigatorが要素、テキスト、処理命令、またはコメント ノードに配置されている場合にのみ有効です。

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

適用対象