XPathNavigator.InsertAfter 方法

定義

在目前選取的節點之後建立新的同層級節點。

多載

InsertAfter(XPathNavigator)

使用所指定 XPathNavigator 物件中的節點,在目前選取的節點之後建立新的同層級節點。

InsertAfter(XmlReader)

使用指定之 XmlReader 物件的 XML 內容,在目前選取的節點之後建立新的同層級節點。

InsertAfter()

傳回 XmlWriter 物件,此物件用來在目前選取的節點之後建立新的同層級節點。

InsertAfter(String)

使用指定的 XML 字串,在目前選取的節點之後建立新的同層級節點。

InsertAfter(XPathNavigator)

使用所指定 XPathNavigator 物件中的節點,在目前選取的節點之後建立新的同層級節點。

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

參數

newSibling
XPathNavigator

位在要做為新同層級節點加入之節點上的 XPathNavigator 物件。

例外狀況

XPathNavigator 物件參數為 null

XPathNavigator 的位置不允許在目前節點後插入新的同層級節點。

範例

在下列範例中,會使用指定物件中包含的 XPathNavigator 節點,在檔案中 contosoBooks.xml 第一 book 個專案的子項目後面 price 插入新的 pages 專案。 指定 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->InsertAfter(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.InsertAfter(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.InsertAfter(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>  

備註

以下是使用 InsertAfter 方法時要考慮的重要注意事項。

適用於

InsertAfter(XmlReader)

使用指定之 XmlReader 物件的 XML 內容,在目前選取的節點之後建立新的同層級節點。

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

參數

newSibling
XmlReader

XmlReader 物件位於新同層級節點的 XML 資料上。

例外狀況

XmlReader 物件處於錯誤狀態或已關閉。

XmlReader 物件參數為 null

XPathNavigator 的位置不允許在目前節點後插入新的同層級節點。

XmlReader 物件參數的 XML 內容語式不正確。

範例

在下列範例中,會使用 XmlReader 指定的 物件,在檔案中 contosoBooks.xml 第一個專案 book 的子項目後面 price 插入新的 pages 元素。 指定 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->InsertAfter(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.InsertAfter(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.InsertAfter(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>  

備註

以下是使用 InsertAfter 方法時要考慮的重要注意事項。

適用於

InsertAfter()

傳回 XmlWriter 物件,此物件用來在目前選取的節點之後建立新的同層級節點。

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

傳回

XmlWriter

XmlWriter 物件,用來在目前選取的節點之後建立新的同層級節點。

例外狀況

XPathNavigator 的位置不允許在目前節點的後面插入新的同層級節點。

範例

在下列範例中,會使用 XmlWriter 方法所 InsertAfter 傳回的物件,在檔案中 contosoBooks.xml 第一 book 個元素的子項目後面 price 插入新的 pages 元素。

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->InsertAfter();
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.InsertAfter();
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.InsertAfter()
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>  

備註

以下是使用 InsertAfter 方法時要考慮的重要注意事項。

適用於

InsertAfter(String)

使用指定的 XML 字串,在目前選取的節點之後建立新的同層級節點。

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

參數

newSibling
String

新的同層級節點之 XML 資料字串。

例外狀況

XML 字串參數為 null

XPathNavigator 的位置不允許在目前節點後插入新的同層級節點。

XML 字串參數的語式不正確。

範例

在下列範例中,會在 檔案中第一 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->InsertAfter("<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.InsertAfter("<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.InsertAfter("<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 節點的字串為 InsertAfter("<book/>") 。 在目前節點的文位元組點為 InsertAfter("book") 之後插入文字 「book」 的字串。 如果 XML 字串包含多個節點,則會新增所有節點。

以下是使用 InsertAfter 方法時要考慮的重要注意事項。

適用於