XmlNode.PrependChild(XmlNode) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将指定的节点添加到此节点的子节点列表的开头。
public:
virtual System::Xml::XmlNode ^ PrependChild(System::Xml::XmlNode ^ newChild);
public virtual System.Xml.XmlNode PrependChild(System.Xml.XmlNode newChild);
public virtual System.Xml.XmlNode? PrependChild(System.Xml.XmlNode newChild);
abstract member PrependChild : System.Xml.XmlNode -> System.Xml.XmlNode
override this.PrependChild : System.Xml.XmlNode -> System.Xml.XmlNode
Public Overridable Function PrependChild (newChild As XmlNode) As XmlNode
参数
- newChild
- XmlNode
要添加的节点。 要添加的节点的所有内容将移动到指定位置。
返回
添加的节点。
例外
示例
以下示例将新节点添加到 XML 文档。
using System;
using System.IO;
using System.Xml;
public class Sample {
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
XmlNode root = doc.DocumentElement;
//Create a new node.
XmlElement elem = doc.CreateElement("price");
elem.InnerText="19.95";
//Add the node to the document.
root.PrependChild(elem);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
Dim doc As New XmlDocument()
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"</book>")
Dim root As XmlNode = doc.DocumentElement
'Create a new node.
Dim elem As XmlElement = doc.CreateElement("price")
elem.InnerText = "19.95"
'Add the node to the document.
root.PrependChild(elem)
Console.WriteLine("Display the modified XML...")
doc.Save(Console.Out)
End Sub
End Class
注解
newChild如果树中已存在,则会将其从其原始位置中删除,并将其添加到其目标位置。 有关插入节点的详细信息,请参阅 将节点插入 XML 文档。
如果要插入的节点是从另一个文档创建的,则可以使用该 XmlDocument.ImportNode 节点将节点导入到当前文档。 然后,可以将导入的节点插入到当前文档中。
此方法是文档对象模型(DOM)的Microsoft扩展。
继承者说明
在派生类中重写 PrependChild 时,若要正确引发事件,必须调用 PrependChild 基类的方法。