System.Xml.XmlWriter.WriteProcessingInstruction Method

When overridden in a derived class, writes out a processing instruction with a space between the name and text as follows: <?name text?>.

Syntax

public abstract void WriteProcessingInstruction (string name, string text)

Parameters

text
The text to include in the processing instruction.
name
The name of the processing instruction.
text
The text to include in the processing instruction.

Exceptions

TypeReason
ArgumentException

The text would result in a non-well formed XML document.

- or -

name is null or string.Empty.

- or -

This method is being used to create an XML declaration after XmlWriter.WriteStartDocument has already been called.

InvalidOperationExceptionThe XmlWriter.WriteState is WriteState.Closed.

Remarks

This method can be used to write the XML declaration (rather than XmlWriter.WriteStartDocument). This could result in the encoding attribute being incorrectly written. For example, the following C# code would result in an invalid XML document because the default encoding is UTF-8.

Example

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

If text is either null or String.Empty, this method writes a ProcessingInstruction with no data content, for example <?name?>.

If text contains an invalid sequence of "?>", the System.Xml.XmlWriter can either throw an ArgumentException (System.Xml.XmlTextWriter objects) or insert a space "? >" to avoid writing invalid XML (System.Xml.XmlWriter objects created by the erload:System.Xml.XmlWriter.Create method).

For the asynchronous version of this method, see XmlWriter.WriteProcessingInstructionAsync(string, string).

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0