XmlProcessingInstruction Class

Overview

The XmlProcessingInstruction class represents a processing instruction, which can be inserted in an XML document to store application-specific information.

Namespace: System.Xml

Assembly: System.Xml.dll

Syntax

public sealed class XmlProcessingInstruction : XmlNode

Constructors

XmlProcessingInstruction(string target, string data)Initializes a new instance with the specified target and data.
XmlProcessingInstruction(string target, string data, XmlDocument doc)Initializes a new instance and attaches it to the specified XmlDocument.

Properties

TargetGets the target of the processing instruction.
DataGets or sets the content of the processing instruction.
NodeTypeReturns XmlNodeType.ProcessingInstruction.

Methods

CloneNode(bool deep)Creates a duplicate of this node.
WriteTo(XmlWriter w)Writes this node to the specified XmlWriter.
WriteContentTo(XmlWriter w)Writes the content of this node to an XmlWriter.

Remarks

Processing instructions are used to convey information to applications processing the XML. The Target specifies the application or processor, while Data contains the instruction.

Typical usage includes XML stylesheet declarations:

<?xml-stylesheet type="text/xsl" href="style.xsl"?>

Example

The following example creates an XML document with a processing instruction that references an XSL stylesheet.

using System;
using System.Xml;

class Program
{
    static void Main()
    {
        XmlDocument doc = new XmlDocument();

        // Create a processing instruction.
        XmlProcessingInstruction pi = doc.CreateProcessingInstruction(
            "xml-stylesheet", "type=\"text/xsl\" href=\"style.xsl\"");

        // Insert the processing instruction before the root element.
        doc.AppendChild(pi);

        // Add a root element.
        XmlElement root = doc.CreateElement("catalog");
        doc.AppendChild(root);

        // Save to a string.
        Console.WriteLine(doc.OuterXml);
    }
}

See also