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

定義

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

public:
 virtual void InsertElementAfter(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI, System::String ^ value);
public virtual void InsertElementAfter (string prefix, string localName, string namespaceURI, string value);
public virtual void InsertElementAfter (string? prefix, string localName, string? namespaceURI, string? value);
abstract member InsertElementAfter : string * string * string * string -> unit
override this.InsertElementAfter : string * string * string * string -> unit
Public Overridable Sub InsertElementAfter (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 では、編集はサポートされていません。

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

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

navigator->InsertElementAfter(navigator->Prefix, "pages", navigator->LookupNamespace(navigator->Prefix), "100");

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

navigator.InsertElementAfter(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.InsertElementAfter(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 値は、 メソッドまたは LookupNamespace メソッドをLookupPrefix使用して取得できます。 たとえば、次の構文では、スコープ内名前空間 xmlns:bk="http://www.contoso.com/books"を使用して兄弟要素を挿入します。

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

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

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

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

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

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

適用対象