Export (0) Print
Expand All

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?>.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

'Declaration
Public MustOverride Sub WriteProcessingInstruction ( _
	name As String, _
	text As String _
)

Parameters

name
Type: System.String
The name of the processing instruction.
text
Type: System.String
The text to include in the processing instruction.

ExceptionCondition
ArgumentException

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

name is either Nothing or String.Empty.

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

This method can be used to write the XML declaration (rather than 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.

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

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

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

The following example navigates through the stream to determine the current node type, and then uses XmlWriter to output the XmlReader content.


Dim output As StringBuilder = New StringBuilder()

Dim xmlString As String = "<?xml version='1.0'?>" & _
                "<!-- This is a sample XML document -->" & _
                "<Items>" & _
                  "<Item>test with a child element <more/> stuff</Item>" & _
                "</Items>"
' Create an XmlReader
Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
    Dim ws As XmlWriterSettings = New XmlWriterSettings()
    ws.Indent = True
    Using writer As XmlWriter = XmlWriter.Create(output, ws)

        ' Parse the file and display each of the nodes.
        While reader.Read()
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    writer.WriteStartElement(reader.Name)
                Case XmlNodeType.Text
                    writer.WriteString(reader.Value)
                Case XmlNodeType.XmlDeclaration
                Case XmlNodeType.ProcessingInstruction
                    writer.WriteProcessingInstruction(reader.Name, reader.Value)
                Case XmlNodeType.Comment
                    writer.WriteComment(reader.Value)
                Case XmlNodeType.EndElement
                    writer.WriteFullEndElement()
            End Select
        End While
    End Using
End Using
OutputTextBlock.Text = output.ToString()


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft