XPathNavigator.PrependChild 方法

定義

在目前節點的子節點清單開頭建立新的子節點。

多載

PrependChild()

傳回 XmlWriter 物件,此物件用來在目前節點的子節點清單開頭建立新的子節點。

PrependChild(String)

使用指定的 XML 字串,在目前節點的子節點清單開頭建立新的子節點。

PrependChild(XmlReader)

使用指定之 XmlReader 物件的 XML 內容,在目前節點的子節點清單開頭建立新的子節點。

PrependChild(XPathNavigator)

使用指定 XPathNavigator 物件中的節點,在目前節點的子節點清單開頭建立新的子節點。

PrependChild()

傳回 XmlWriter 物件,此物件用來在目前節點的子節點清單開頭建立新的子節點。

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

傳回

XmlWriter

用以在目前節點的子節點清單開頭建立新子節點的 XmlWriter 物件。

例外狀況

目前放置 XPathNavigator 的節點不允許預先規劃新子節點。

範例

在下列範例中,新的 pages 子專案會使用 從 PrependChild 方法傳回的物件,在檔案 XmlWritercontosoBooks.xml 第一 book 個專案的子專案清單開頭加上。

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

XmlWriter^ pages = navigator->PrependChild();
pages->WriteElementString("pages", "100");
pages->Close();

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

XmlWriter pages = navigator.PrependChild();
pages.WriteElementString("pages", "100");
pages.Close();

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

Dim pages As XmlWriter = navigator.PrependChild()
pages.WriteElementString("pages", "100")
pages.Close()

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>  

備註

在子節點前面加上新節點,會將新節點新增至目前節點之子節點清單的開頭。 例如,當元素有三個子節點存在時,前置節點會變成第一個子節點。 如果沒有子節點存在,則會建立新的子節點。

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

適用於

PrependChild(String)

使用指定的 XML 字串,在目前節點的子節點清單開頭建立新的子節點。

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

參數

newChild
String

新的子節點之 XML 資料字串。

例外狀況

XML 字串參數為 null

XPathNavigator 所在的目前節點不允許在前面加上新的子節點。

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

範例

在下列範例中,新的 pages 子專案會前面加上檔案中 contosoBooks.xml 第一 book 個專案之子項目清單的開頭。

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

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

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

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 節點的字串為 PrependChild("<book/>") 。 將文字 「book」 附加至目前節點文位元組點的字串為 PrependChild("book") 。 如果 XML 字串包含多個節點,則會新增所有節點。

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

適用於

PrependChild(XmlReader)

使用指定之 XmlReader 物件的 XML 內容,在目前節點的子節點清單開頭建立新的子節點。

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

參數

newChild
XmlReader

位於新的子節點之 XML 資料上的 XmlReader 物件。

例外狀況

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

XmlReader 物件參數為 null

XPathNavigator 所在的目前節點不允許在前面加上新的子節點。

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

範例

在下列範例中,會使用 XmlReader 指定的 物件,在檔案中 contosoBooks.xml 第一 book 個專案的子專案清單開頭加上新的 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");

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

navigator->PrependChild(pages);

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

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

navigator.PrependChild(pages);

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

Dim pages As XmlReader = XmlReader.Create(New StringReader("<pages xmlns='http://www.contoso.com/books'>100</pages>"))

navigator.PrependChild(pages)

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>  

備註

在子節點前面加上新節點,會將新節點新增至目前節點之子節點清單的開頭。 例如,當元素有三個子節點存在時,前置節點會變成第一個子節點。 如果沒有子節點存在,則會建立新的子節點。

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

適用於

PrependChild(XPathNavigator)

使用指定 XPathNavigator 物件中的節點,在目前節點的子節點清單開頭建立新的子節點。

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

參數

newChild
XPathNavigator

XPathNavigator 物件,位於要以新的子節點形式加入的節點上。

例外狀況

XPathNavigator 物件參數為 null

XPathNavigator 所在的目前節點不允許在前面加上新的子節點。

範例

在下列範例中,新的 pages 子專案會使用指定的 物件中包含的 XPathNavigator 節點,在檔案中 contosoBooks.xml 第一 book 個元素的子項目清單開頭加上。 會 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");

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

navigator->PrependChild(childNodesNavigator);

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

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

navigator.PrependChild(childNodesNavigator);

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

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.PrependChild(childNodesNavigator)

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>  

備註

在子節點前面加上新節點,會將新節點新增至目前節點之子節點清單的開頭。 例如,當元素有三個子節點存在時,前置節點會變成第一個子節點。 如果沒有子節點存在,則會建立新的子節點。

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

適用於