XmlWriter.WriteProcessingInstruction(String, String) XmlWriter.WriteProcessingInstruction(String, String) XmlWriter.WriteProcessingInstruction(String, String) XmlWriter.WriteProcessingInstruction(String, String) Method

정의

파생 클래스에서 재정의되면 <?name text?> 같이 이름과 텍스트 사이에 공백이 있는 처리 명령을 작성합니다.When overridden in a derived class, writes out a processing instruction with a space between the name and text as follows: <?name text?>.

public:
 abstract void WriteProcessingInstruction(System::String ^ name, System::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 String String String

처리 명령의 이름입니다.The name of the processing instruction.

text
String String String String

처리 명령에 포함할 텍스트입니다.The text to include in the processing instruction.

예외

텍스트로 인해 XML 문서가 제대로 구성되지 않은 경우The text would result in a non-well formed XML document.

namenull 또는 String.Empty입니다.name is either null or String.Empty.

WriteStartDocument()가 이미 호출된 후 이 메서드를 사용하여 XML 선언을 만드는 경우This method is being used to create an XML declaration after WriteStartDocument() has already been called.

이전 비동기 작업이 완료되기 전에 XmlWriter 메서드가 호출되었습니다.An XmlWriter method was called before a previous asynchronous operation finished. 이 경우 “비동기 작업이 이미 진행 중입니다.” 메시지를 나타내며 InvalidOperationException이 throw됩니다.In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

예제

다음 예제에서는 책을 나타내는 XML 파일을 씁니다.The following example writes an XML file representing a book.

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
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 'Main 
End Class 'Sample

설명

이 메서드를 사용 하 여 XML 선언을 쓸 수 있습니다 (대신 WriteStartDocument).This method can be used to write the XML declaration (rather than WriteStartDocument). 이 제대로 작성 되지 인코딩 특성이 될 수 있습니다.This could result in the encoding attribute being incorrectly written. 예를 들어 다음 C# 코드 기본 인코딩은 u t F-8 때문에 잘못 된 XML 문서에서 초래 합니다.For example, the following C# code would result in an invalid XML document because the default encoding is UTF-8.

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

경우 textnull 또는 String.Empty,이 메서드는 작성을 ProcessingInstruction 예를 들어 콘텐츠, 데이터가 없는 <? 이름? >.If text is either null or String.Empty, this method writes a ProcessingInstruction with no data content, for example <?name?>.

텍스트에 잘못 된 시퀀스를 포함 하는 경우 "? >", XmlWriter throw 하거나 수를 ArgumentException (XmlTextWriter 개체) 또는 공백을 삽입 "?If text contains an invalid sequence of "?>", the XmlWriter can either throw an ArgumentException (XmlTextWriter objects) or insert a space "? > "잘못 된 XML을 작성 하지 않으려면 (XmlWriter 개체에 의해 생성 된 Create 메서드).>" to avoid writing invalid XML (XmlWriter objects created by the Create method).

이 메서드의 비동기 버전을 참조 하세요. WriteProcessingInstructionAsync합니다.For the asynchronous version of this method, see WriteProcessingInstructionAsync.

적용 대상