StringWriter sw = new StringWriter();

XmlTextWriter w = new XmlTextWriter(sw);
w.IndentChar = '\t';
w.Indentation = 1;
w.Formatting = Formatting.Indented;

w.WriteProcessingInstruction("xml", @"version=""1.0""");
w.WriteStartElement("Document");
    w.WriteStartElement("Frame");
    w.WriteAttributeString("Url", 
                       "http://www.w3schools.com/tags/planets.htm");
        w.WriteStartElement("Frame");
        w.WriteAttributeString("Url", 
                       "http://www.w3schools.com/tags/venus.htm");
        w.WriteEndElement();
        w.WriteStartElement("Frame");
        w.WriteAttributeString("Url", 
                       "http://www.w3schools.com/tags/sun.htm");
        w.WriteEndElement();
        w.WriteStartElement("Frame");
        w.WriteAttributeString("Url", 
                       "http://www.w3schools.com/tags/mercur.htm");
        w.WriteEndElement();

    w.WriteEndElement();
w.WriteEndElement();

Example 3: XML processing instruction.

Back to Article