CSharpFeeds - All your C# feeds in one place.

Sponsors

Monday, September 18, 2006

Use StringWriter and XmlTextWriter to Generate XML Documents on Fly

by Keyvan Nayyeri via Keyvan Nayyeri on 9/18/2006 5:38:20 AM

This is just my favorite way to generate XML files on fly.  Often I need to create an XML file in memory independent from a file.  To do this I use StringWriter in conjunction with XmlTextWriter to generate my XML file as string.

This approach has some issues for encoding since .NET uses UTF-16 encoding for strings.  You can follow Robert's post and its comments for more information.

The process to generate an XML file with this approach is so easy: create a StringWriter object and pass it as a TextWriter stream to XmlTextWriter constructor to create a new instance then generate your XML via this XmlTextWriter and finally get its string equivalent from StringWriter.ToString() method.

As an example look at my sample below:

public class MyClass

{

    public static string GetXml(bool addId, int id)

    {

        StringWriter stringWriter = new StringWriter();

 

        XmlTextWriter xmltextWriter =

            new XmlTextWriter(stringWriter);

        xmltextWriter.Formatting = Formatting.Indented;

 

        // Start document

        xmltextWriter.WriteStartDocument();

 

        xmltextWriter.WriteStartElement("myxml");

        if (addId)

            xmltextWriter.WriteAttributeString

                ("id", id.ToString());

 

        xmltextWriter.WriteAttributeString

            ("attribute1", "value1");

        xmltextWriter.WriteAttributeString

            ("attribute2", "value2");

 

        xmltextWriter.WriteStartElement

            ("myelement");

        xmltextWriter.WriteAttributeString

            ("attribute3", "value3");

 

 

        xmltextWriter.WriteElementString

            ("subelement", "This is a value!");

 

        xmltextWriter.WriteEndElement();  

 

        // End document

        xmltextWriter.WriteEndElement();

 

        xmltextWriter.Flush();

        xmltextWriter.Close();

        stringWriter.Flush();

 

        return stringWriter.ToString();

    } 

}

To test my code I use a Console application:

static void Main(string[] args)

{

    Console.Title = "Create XML File on Fly";

    Console.WriteLine(MyClass.GetXml(true, 5));

    Console.ReadLine();

}

Create XML File on Fly

Also I can use this string value to load it into an XmlDocument object and manipulate my document there.

static void Main(string[] args)

{

    Console.Title = "Create XML File on Fly";

 

    XmlDocument xml = new XmlDocument();

    xml.LoadXml(MyClass.GetXml(true, 5));

    XmlNodeList nodes = xml.SelectNodes

        ("/myxml/myelement");

 

    Console.WriteLine(nodes[0].

        Attributes["attribute3"].InnerText);

 

    Console.ReadLine();

}

Now playing: Richard Clayderman - Souvenirs D'enfance

email it!bookmark it!digg it!

Original Post: Use StringWriter and XmlTextWriter to Generate XML Documents on Fly

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us