XPathNavigator.AppendChild 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在目前節點的子節點清單結尾建立新的子節點。
多載
| AppendChild() |
傳回 XmlWriter 物件,此物件用來在目前節點的子節點清單結尾建立一或多個新的子節點。 |
| AppendChild(String) |
使用指定的 XML 資料字串,在目前節點的子節點清單結尾建立新的子節點。 |
| AppendChild(XmlReader) |
使用指定 XmlReader 物件的 XML 內容,在目前節點的子節點清單結尾建立新的子節點。 |
| AppendChild(XPathNavigator) |
使用 XPathNavigator 中指定的節點,在目前節點的子節點清單結尾建立新的子節點。 |
AppendChild()
傳回 XmlWriter 物件,此物件用來在目前節點的子節點清單結尾建立一或多個新的子節點。
public:
virtual System::Xml::XmlWriter ^ AppendChild();
public virtual System.Xml.XmlWriter AppendChild ();
abstract member AppendChild : unit -> System.Xml.XmlWriter
override this.AppendChild : unit -> System.Xml.XmlWriter
Public Overridable Function AppendChild () As XmlWriter
傳回
XmlWriter 物件,用來在目前節點的子節點清單結尾建立新的子節點。
例外狀況
XPathNavigator 所在的目前節點不是根節點或項目節點。
範例
在下列範例中,使用 XmlWriter 方法 AppendChild 傳回的物件,將新的 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");
XmlWriter^ pages = navigator->AppendChild();
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.AppendChild();
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.AppendChild()
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>
備註
附加子節點會將新節點新增至目前節點之子節點清單的結尾。 例如,當元素存在三個子節點時,第一個附加的節點會變成第四個子節點。 如果沒有子節點存在,則會建立新的子節點。
以下是使用 AppendChild 方法時要考慮的重要注意事項。
只有在 位於根節點或專案節點上時 XPathNavigator ,方法 AppendChild 才有效。
方法 AppendChild 不會影響 的位置 XPathNavigator 。
您可以將多個節點寫入寫入器。 所有節點都會附加至目前節點之子節點清單的結尾。
適用於
AppendChild(String)
使用指定的 XML 資料字串,在目前節點的子節點清單結尾建立新的子節點。
public:
virtual void AppendChild(System::String ^ newChild);
public virtual void AppendChild (string newChild);
abstract member AppendChild : string -> unit
override this.AppendChild : string -> unit
Public Overridable Sub AppendChild (newChild As String)
參數
- newChild
- String
新的子節點之 XML 資料字串。
例外狀況
XML 資料字串參數為 null。
XPathNavigator 所在的目前節點不是根節點或項目節點。
XML 資料字串參數的語式不正確。
範例
在下列範例中,會將新的 pages 項目子系附加至 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->AppendChild("<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.AppendChild("<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.AppendChild("<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 節點的字串為 AppendChild("<book/>") 。 將文字 「book」 附加至目前節點文位元組點的字串為 AppendChild("book") 。 如果 XML 字串包含多個節點,則會新增所有節點。
以下是使用 AppendChild 方法時要考慮的重要注意事項。
只有在 位於根節點或專案節點上時 XPathNavigator ,方法 AppendChild 才有效。
方法 AppendChild 不會影響 的位置 XPathNavigator 。
適用於
AppendChild(XmlReader)
使用指定 XmlReader 物件的 XML 內容,在目前節點的子節點清單結尾建立新的子節點。
public:
virtual void AppendChild(System::Xml::XmlReader ^ newChild);
public virtual void AppendChild (System.Xml.XmlReader newChild);
abstract member AppendChild : System.Xml.XmlReader -> unit
override this.AppendChild : System.Xml.XmlReader -> unit
Public Overridable Sub AppendChild (newChild As XmlReader)
參數
例外狀況
XmlReader 物件處於錯誤狀態或已關閉。
XmlReader 物件參數為 null。
XPathNavigator 所在的目前節點不是根節點或項目節點。
XmlReader 物件參數的 XML 內容語式不正確。
範例
在下列範例中,會使用 XmlReader 指定的 物件,將新的 pages 子專案附加至檔案中 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");
XmlReader^ pages = XmlReader::Create(gcnew StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));
navigator->AppendChild(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.AppendChild(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.AppendChild(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>
備註
附加子節點會將新節點新增至目前節點的子節點清單結尾。 例如,當元素有三個子節點時,附加的節點會變成第四個子節點。 如果沒有子節點存在,則會建立新的子節點。
以下是使用 AppendChild 方法時要考慮的重要注意事項。
XmlReader如果物件位於 XML 節點序列上,則會加入序列中的所有節點。
只有在 位於根節點或專案節點上時 XPathNavigator ,方法 AppendChild 才有效。
方法 AppendChild 不會影響 的位置 XPathNavigator 。
適用於
AppendChild(XPathNavigator)
使用 XPathNavigator 中指定的節點,在目前節點的子節點清單結尾建立新的子節點。
public:
virtual void AppendChild(System::Xml::XPath::XPathNavigator ^ newChild);
public virtual void AppendChild (System.Xml.XPath.XPathNavigator newChild);
abstract member AppendChild : System.Xml.XPath.XPathNavigator -> unit
override this.AppendChild : System.Xml.XPath.XPathNavigator -> unit
Public Overridable Sub AppendChild (newChild As XPathNavigator)
參數
- newChild
- XPathNavigator
XPathNavigator 物件,位於要以新的子節點形式加入的節點上。
例外狀況
XPathNavigator 物件參數為 null。
XPathNavigator 所在的目前節點不是根節點或項目節點。
範例
在下列範例中,新的 pages 子專案會附加至檔案中 contosoBooks.xml 第一 book 個元素的子專案清單,並使用指定的 中包含的 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");
XmlDocument^ childNodes = gcnew XmlDocument();
childNodes->Load(gcnew StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));
XPathNavigator^ childNodesNavigator = childNodes->CreateNavigator();
if (childNodesNavigator->MoveToChild("pages", "http://www.contoso.com/books"))
{
navigator->AppendChild(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();
if(childNodesNavigator.MoveToChild("pages", "http://www.contoso.com/books"))
{
navigator.AppendChild(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()
If childNodesNavigator.MoveToChild("pages", "http://www.contoso.com/books") Then
navigator.AppendChild(childNodesNavigator)
End If
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>
備註
附加子節點會將新節點新增至目前節點的子節點清單結尾。 例如,當元素有三個子節點時,附加的節點會變成第四個子節點。 如果沒有子節點存在,則會建立新的子節點。
以下是使用 AppendChild 方法時要考慮的重要注意事項。
XPathNavigator如果物件位於 XML 節點序列上,則會加入序列中的所有節點。
只有在 位於根節點或專案節點上時 XPathNavigator ,方法 AppendChild 才有效。
方法 AppendChild 不會影響 的位置 XPathNavigator 。