共用方式為


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 Extensible Markup Language (XML) 1.0 建議,只有在 EntityReference 節點不是 Attribute 節點的子節點時,才允許 ProcessingInstruction 節點。

適用於