When overridden in a derived class, writes out a processing instruction with a space between the name and text as follows: <?name text?>.
Type Reason 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.
InvalidOperationException The XmlWriter.WriteState is WriteState.Closed.
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).