objPI = doc.createProcessingInstruction("xml","version='1.0' encoding='utf-8'")
doc.insertBefore (objPI, doc.childNodes[0])

解决方案 »

  1.   

    尽管此方法在文档的上下文中创建新对象,但它并不自动将新对象添加到文档树。若要添加新对象,必须显式调用节点插入方法之一。根据 W3C 可扩展标记语言 (XML) 1.0 建议 (www.w3.org/TR/1998/REC-xml-19980210),ProcessingInstruction 节点可包含在 Document 节点和 Element 节点中,并且当 EntityReference 节点不是 Attribute 节点的子级时,ProcessingInstruction 节点还可包含在 EntityReference 节点中。示例
    [Visual Basic, C#] 下面的示例创建一个 ProcessingInstruction 节点并将其添加到文档中。[Visual Basic] 
    Imports System
    Imports System.IO
    Imports System.Xmlpublic class Sample  public shared sub Main()
      
        Dim doc as XmlDocument = new XmlDocument()    ' Create a procesing instruction.
        Dim newPI as XmlProcessingInstruction 
        Dim PItext as String = "type='text/xsl' href='book.xsl'"
        newPI = doc.CreateProcessingInstruction("xml-stylesheet", PItext)    ' Display the target and data information.
        Console.WriteLine("<?{0} {1}?>", newPI.Target, newPI.Data)    ' Add the processing instruction node to the document.
        doc.AppendChild(newPI)  end sub
    end class
      

  2.   

    http://msdn.microsoft.com/library/en-us/xmlsdk/html/278cb27e-7e61-4d6e-84f0-11516f3dd5e8.asp