XPathNavigator.InsertBefore メソッド

定義

現在選択されているノードの前に新しい兄弟ノードを作成します。

オーバーロード

InsertBefore()

現在選択されているノードの前に新しい兄弟ノードを作成するために使用する XmlWriter オブジェクトを返します。

InsertBefore(String)

指定された XML 文字列を使用して、現在選択されているノードの前に新しい兄弟ノードを作成します。

InsertBefore(XmlReader)

指定された XmlReader オブジェクトの XML の内容を使用して、現在選択されているノードの前に新しい兄弟ノードを作成します。

InsertBefore(XPathNavigator)

指定された XPathNavigator のノードを使用して、現在選択されているノードの前に新しい兄弟ノードを作成します。

InsertBefore()

現在選択されているノードの前に新しい兄弟ノードを作成するために使用する XmlWriter オブジェクトを返します。

public:
 virtual System::Xml::XmlWriter ^ InsertBefore();
public virtual System.Xml.XmlWriter InsertBefore ();
abstract member InsertBefore : unit -> System.Xml.XmlWriter
override this.InsertBefore : unit -> System.Xml.XmlWriter
Public Overridable Function InsertBefore () As XmlWriter

戻り値

XmlWriter

現在選択されているノードの前に新しい兄弟ノードを作成するために使用する XmlWriter オブジェクト。

例外

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

XPathNavigator では、編集はサポートされていません。

次の例では、メソッドによって返されるオブジェクトをprice使用してXmlWriter、ファイル内の最初bookの要素の子要素の前に新しいpages要素contosoBooks.xmlInsertBefore挿入されます。

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^ pages = navigator->InsertBefore();
pages->WriteElementString("pages", "100");
pages->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 pages = navigator.InsertBefore();
pages.WriteElementString("pages", "100");
pages.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 pages As XmlWriter = navigator.InsertBefore()
pages.WriteElementString("pages", "100")
pages.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>  

注釈

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

  • 新しい兄弟ノードは、オブジェクトのXmlWriterメソッドがClose呼び出されるまで挿入されません。

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

  • このメソッドは InsertBeforeXPathNavigator.

適用対象

InsertBefore(String)

指定された XML 文字列を使用して、現在選択されているノードの前に新しい兄弟ノードを作成します。

public:
 virtual void InsertBefore(System::String ^ newSibling);
public virtual void InsertBefore (string newSibling);
abstract member InsertBefore : string -> unit
override this.InsertBefore : string -> unit
Public Overridable Sub InsertBefore (newSibling As String)

パラメーター

newSibling
String

新しい兄弟ノード用の XML データ文字列。

例外

XML 文字列パラメーターは null です。

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

XPathNavigator では、編集はサポートされていません。

XML 文字列パラメーターは整形式ではありません。

次の例では、新しい pages 要素が price ファイル内の最初の book 要素の 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->InsertBefore("<pages>100</pages>");

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.InsertBefore("<pages>100</pages>");

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.InsertBefore("<pages>100</pages>")

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>  

注釈

新しい要素ノードを作成するには、XML 文字列パラメーターにすべての XML 構文を含めます。 新しい book ノードの文字列は InsertBefore("<book/>"). 現在のノードのテキスト ノードの前にテキスト "book" を挿入するための文字列。InsertBefore("book") XML 文字列に複数のノードが含まれている場合は、すべてのノードが追加されます。

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

適用対象

InsertBefore(XmlReader)

指定された XmlReader オブジェクトの XML の内容を使用して、現在選択されているノードの前に新しい兄弟ノードを作成します。

public:
 virtual void InsertBefore(System::Xml::XmlReader ^ newSibling);
public virtual void InsertBefore (System.Xml.XmlReader newSibling);
abstract member InsertBefore : System.Xml.XmlReader -> unit
override this.InsertBefore : System.Xml.XmlReader -> unit
Public Overridable Sub InsertBefore (newSibling As XmlReader)

パラメーター

newSibling
XmlReader

新しい兄弟ノード用の XML データに配置された XmlReader オブジェクト。

例外

XmlReader オブジェクトがエラー状態か、または閉じています。

XmlReader オブジェクトのパラメーターは null です。

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

XPathNavigator では、編集はサポートされていません。

XmlReader オブジェクト パラメーターの XML コンテンツは整形式ではありません。

次の例では、指定されたオブジェクトを使用して、ファイル内のprice最初bookの要素の子要素の前にcontosoBooks.xml新しいpages要素がXmlReader挿入されます。 http://www.contoso.com/books新しい兄弟要素が 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");

XmlReader^ pages = XmlReader::Create(gcnew StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));

navigator->InsertBefore(pages);

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

XmlReader pages = XmlReader.Create(new StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));

navigator.InsertBefore(pages);

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 pages As XmlReader = XmlReader.Create(New StringReader("<pages xmlns='http://www.contoso.com/books'>100</pages>"))

navigator.InsertBefore(pages)

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>  

注釈

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

  • オブジェクトが XmlReader 一連の XML ノード上に配置されている場合は、シーケンス内のすべてのノードが追加されます。

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

  • このメソッドは InsertBeforeXPathNavigator.

適用対象

InsertBefore(XPathNavigator)

指定された XPathNavigator のノードを使用して、現在選択されているノードの前に新しい兄弟ノードを作成します。

public:
 virtual void InsertBefore(System::Xml::XPath::XPathNavigator ^ newSibling);
public virtual void InsertBefore (System.Xml.XPath.XPathNavigator newSibling);
abstract member InsertBefore : System.Xml.XPath.XPathNavigator -> unit
override this.InsertBefore : System.Xml.XPath.XPathNavigator -> unit
Public Overridable Sub InsertBefore (newSibling As XPathNavigator)

パラメーター

newSibling
XPathNavigator

新しい兄弟ノードとして追加するノードに配置されている XPathNavigator オブジェクト。

例外

XPathNavigator オブジェクトのパラメーターは null です。

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

XPathNavigator では、編集はサポートされていません。

次の例では、指定されたオブジェクトに含まれるノードをprice使用して、ファイル内contosoBooks.xmlの最初bookの要素の子要素の前に新しいpages要素がXPathNavigator挿入されます。 http://www.contoso.com/books新しい兄弟要素が 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");

XmlDocument^ childNodes = gcnew XmlDocument();
childNodes->Load(gcnew StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));
XPathNavigator^ childNodesNavigator = childNodes->CreateNavigator();

navigator->InsertBefore(childNodesNavigator);

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

XmlDocument childNodes = new XmlDocument();
childNodes.Load(new StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));
XPathNavigator childNodesNavigator = childNodes.CreateNavigator();

navigator.InsertBefore(childNodesNavigator);

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 childNodes As XmlDocument = New XmlDocument()
childNodes.Load(New StringReader("<pages xmlns='http://www.contoso.com/books'>100</pages>"))
Dim childNodesNavigator As XPathNavigator = childNodes.CreateNavigator()

navigator.InsertBefore(childNodesNavigator)

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>  

注釈

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

  • オブジェクトが XPathNavigator 一連の XML ノード上に配置されている場合は、シーケンス内のすべてのノードが追加されます。

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

  • このメソッドは InsertBeforeXPathNavigator.

適用対象