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

定義

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

public:
 virtual void PrependChildElement(System::String ^ prefix, System::String ^ localName, System::String ^ namespaceURI, System::String ^ value);
public virtual void PrependChildElement (string prefix, string localName, string namespaceURI, string value);
public virtual void PrependChildElement (string? prefix, string localName, string? namespaceURI, string? value);
abstract member PrependChildElement : string * string * string * string -> unit
override this.PrependChildElement : string * string * string * string -> unit
Public Overridable Sub PrependChildElement (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の要素の子要素のリストの先頭に新しい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->PrependChildElement(navigator->Prefix, "pages", navigator->LookupNamespace(navigator->Prefix), "100");

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.PrependChildElement(navigator.Prefix, "pages", navigator.LookupNamespace(navigator.Prefix), "100");

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.PrependChildElement(navigator.Prefix, "pages", navigator.LookupNamespace(navigator.Prefix), "100")

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>  

注釈

子ノードの前に、現在のノードの子ノードの一覧の先頭に新しいノードが追加されます。 たとえば、要素に対して 3 つの子ノードが存在する場合、先頭のノードが最初の子ノードになります。 子ノードが存在しない場合は、新しい子ノードが作成されます。

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

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

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

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

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

  • メソッドは PrependChildElement 、 が要素ノードに配置されている場合 XPathNavigator にのみ有効です。

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

適用対象