CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Tuesday, October 03, 2006

Use StringWriter and XmlTextWriter to Generate XML Documents on Fly

Use StringWriter and XmlTextWriter to Generate XML Documents on Fly

Monday, September 18, 2006, 11:08:20 AM | Keyvan NayyeriGo to full article
This is just my favoriteway 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 asstring. 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...(read more)

Post a Comment