XmlWriter.WriteProcessingInstruction(String, String) 메서드

정의

파생 클래스에서 재정의되면 <?name text?> 같이 이름과 텍스트 사이에 공백이 있는 처리 명령을 작성합니다.

public:
 abstract void WriteProcessingInstruction(System::String ^ name, System::String ^ text);
public abstract void WriteProcessingInstruction (string name, string text);
public abstract void WriteProcessingInstruction (string name, string? text);
abstract member WriteProcessingInstruction : string * string -> unit
Public MustOverride Sub WriteProcessingInstruction (name As String, text As String)

매개 변수

name
String

처리 명령의 이름입니다.

text
String

처리 명령에 포함할 텍스트입니다.

예외

텍스트로 인해 XML 문서가 제대로 구성되지 않은 경우

namenull 또는 String.Empty입니다.

WriteStartDocument()가 이미 호출된 후 이 메서드를 사용하여 XML 선언을 만드는 경우

이전 비동기 작업이 완료되기 전에 XmlWriter 메서드가 호출되었습니다. 이 경우 “비동기 작업이 이미 진행 중입니다.” 메시지를 나타내며 InvalidOperationException이 throw됩니다.

예제

다음 예제에서는 책을 나타내는 XML 파일을 씁니다.

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

public class Sample {

  private const string filename = "sampledata.xml";

  public static void Main() {

     XmlWriterSettings settings = new XmlWriterSettings();
     settings.Indent = true;
     XmlWriter writer = XmlWriter.Create(filename, settings);

     // Write the Processing Instruction node.
     String PItext="type=\"text/xsl\" href=\"book.xsl\"";
     writer.WriteProcessingInstruction("xml-stylesheet", PItext);

     // Write the DocumentType node.
     writer.WriteDocType("book", null , null, "<!ENTITY h \"hardcover\">");

     // Write a Comment node.
     writer.WriteComment("sample XML");

     // Write the root element.
     writer.WriteStartElement("book");

     // Write the genre attribute.
     writer.WriteAttributeString("genre", "novel");

     // Write the ISBN attribute.
     writer.WriteAttributeString("ISBN", "1-8630-014");

     // Write the title.
     writer.WriteElementString("title", "The Handmaid's Tale");

     // Write the style element.
     writer.WriteStartElement("style");
     writer.WriteEntityRef("h");
     writer.WriteEndElement();

     // Write the price.
     writer.WriteElementString("price", "19.95");

     // Write CDATA.
     writer.WriteCData("Prices 15% off!!");

     // Write the close tag for the root element.
     writer.WriteEndElement();

     writer.WriteEndDocument();

     // Write the XML to file and close the writer.
     writer.Flush();
     writer.Close();
  }
}
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml

Public Class Sample
    Private Const filename As String = "sampledata.xml"
    
  Public Shared Sub Main()

     Dim settings As XmlWriterSettings = new XmlWriterSettings()
     settings.Indent = true
     Dim writer As XmlWriter = XmlWriter.Create(filename, settings)
        
     ' Write the Processing Instruction node.
     Dim PItext As String = "type=""text/xsl"" href=""book.xsl"""
     writer.WriteProcessingInstruction("xml-stylesheet", PItext)
        
     'Write the DocumentType node.
     writer.WriteDocType("book", Nothing, Nothing, "<!ENTITY h ""hardcover"">")
        
     ' Write a Comment node.
     writer.WriteComment("sample XML")
        
     ' Write the root element.
     writer.WriteStartElement("book")
        
     ' Write the genre attribute
     writer.WriteAttributeString("genre", "novel")
        
     ' Write the ISBN attribute.
     writer.WriteAttributeString("ISBN", "1-8630-014")
        
     ' Write the title.
     writer.WriteElementString("title", "The Handmaid's Tale")
        
     ' Write the style element.
     writer.WriteStartElement("style")
     writer.WriteEntityRef("h")
     writer.WriteEndElement()
       
     ' Write the price.
     writer.WriteElementString("price", "19.95")
        
     ' Write CDATA.
     writer.WriteCData("Prices 15% off!!")
        
     ' Write the close tag for the root element.
     writer.WriteEndElement()
        
     writer.WriteEndDocument()
        
     ' Write the XML to file and close the writer
     writer.Flush()
     writer.Close()
        
    End Sub
End Class

설명

이 메서드를 사용하여 XML 선언(대신 WriteStartDocument)을 작성할 수 있습니다. 이로 인해 인코딩 특성이 잘못 작성될 수 있습니다. 예를 들어 다음 C# 코드는 기본 인코딩이 UTF-8이므로 잘못된 XML 문서를 생성합니다.

XmlWriter writer = XmlWriter.Create("output.xml");
writer.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-16'");
writer.WriteStartElement("root");
writer.Close();

가 또는 이면 text 이 메서드는 ProcessingInstruction 데이터 콘텐츠가 없는 를 씁니다(예<: ?name?>String.Empty).null

텍스트에 잘못된 시퀀스 "?>"가 포함된 경우 는 XmlWriter ( 개체)를 ArgumentExceptionXmlTextWriter throw하거나 "? >" 잘못된 XML(XmlWriter 메서드에서 만든 개체)을 Create 작성하지 않도록 합니다.

이 메서드의 비동기 버전은 를 참조하세요 WriteProcessingInstructionAsync.

적용 대상