XmlDocument.CreateProcessingInstruction(String, String) 方法

定义

创建一个具有指定名称和数据的 XmlProcessingInstruction

public:
 virtual System::Xml::XmlProcessingInstruction ^ CreateProcessingInstruction(System::String ^ target, System::String ^ data);
public virtual System.Xml.XmlProcessingInstruction CreateProcessingInstruction (string target, string data);
abstract member CreateProcessingInstruction : string * string -> System.Xml.XmlProcessingInstruction
override this.CreateProcessingInstruction : string * string -> System.Xml.XmlProcessingInstruction
Public Overridable Function CreateProcessingInstruction (target As String, data As String) As XmlProcessingInstruction

参数

target
String

处理指令的名称。

data
String

处理指令的数据。

返回

XmlProcessingInstruction

新的 XmlProcessingInstruction

示例

以下示例创建 ProcessingInstruction 节点并将其添加到文档中。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   
   // Create a procesing instruction.
   XmlProcessingInstruction^ newPI;
   String^ PItext = "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 );
}

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();

    // Create a procesing instruction.
    XmlProcessingInstruction newPI;
    String PItext = "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);
  }
}
Imports System.IO
Imports System.Xml

public 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

注解

尽管此方法在文档上下文中创建新对象,但它不会自动将新对象添加到文档树。 若要添加新对象,必须显式调用其中一个节点插入方法。

根据 W3C 可扩展标记语言 (XML) 1.0 建议,仅当 EntityReference 节点不是 Attribute 节点的子节点时,仅允许在 Document、Element 和 EntityReference 节点内使用 ProcessingInstruction 节点。

适用于