XmlNode.InsertBefore(XmlNode, XmlNode) 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将指定的节点紧接着插入指定的引用节点之前。
public:
 virtual System::Xml::XmlNode ^ InsertBefore(System::Xml::XmlNode ^ newChild, System::Xml::XmlNode ^ refChild);public virtual System.Xml.XmlNode InsertBefore(System.Xml.XmlNode newChild, System.Xml.XmlNode refChild);public virtual System.Xml.XmlNode? InsertBefore(System.Xml.XmlNode newChild, System.Xml.XmlNode? refChild);abstract member InsertBefore : System.Xml.XmlNode * System.Xml.XmlNode -> System.Xml.XmlNode
override this.InsertBefore : System.Xml.XmlNode * System.Xml.XmlNode -> System.Xml.XmlNodePublic Overridable Function InsertBefore (newChild As XmlNode, refChild As XmlNode) As XmlNode参数
- newChild
- XmlNode
要插入的节点。
- refChild
- XmlNode
引用节点。 
              newChild 放置在该节点之前。
返回
插入的节点。
例外
示例
以下示例向 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.InsertBefore(elem, root.FirstChild);
    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Option Strict
Option Explicit
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.InsertBefore(elem, root.FirstChild)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class
注解
如果 refChild 为 null,请在子节点列表的末尾插入 newChild 。 在 newChild 之前 refChild,将按相同的顺序插入 的所有内容。 
              newChild如果 已在树中,则会将其从其原始位置删除并添加到其目标位置。 有关插入节点的详细信息,请参阅 将节点插入 XML 文档。
如果要插入的节点是从另一个文档创建的,则可以使用 XmlDocument.ImportNode 将节点导入当前文档。 然后,可以将导入的节点插入到当前文档中。
继承者说明
在派生类中重写 InsertBefore 时,若要正确引发事件,必须调用 InsertBefore 基类的 方法。