Solved

How to write XML data to a string not to a file?

Posted on 2004-09-24
10
233 Views
Last Modified: 2010-04-15
Hi Y'all,

I'm in a bit of a rush here and trying to figure out how to write XML data to a string/buffer rather than a file.  This is what I'm been trying.  Am I way off?

MemoryStream ms = new MemoryStream();
XmlTextWriter tw = new XmlTextWriter(ms, Encoding.UTF8 );
tw.WriteStartDocument();
tw.WriteStartElement("ClientRequest");
tw.WriteString("Test");
tw.WriteEndElement();
tw.WriteEndDocument();
byte[] buf = new byte[ms.Length];
ms.Seek(0,SeekOrigin.Begin);
ms.Read(buf,0,(int)ms.Length);
string request = System.Text.Encoding.UTF8.GetString(buf,0,buf.Length);

Many thanks!

Aaron
0
Comment
Question by:AaronReams
10 Comments
 
LVL 4

Expert Comment

by:thedude112286
Comment Utility
That looks good, just don't forget to call ms.Close() after you get the string to free up the resources.
0
 
LVL 8

Author Comment

by:AaronReams
Comment Utility
it might look good but unfortunately it doesn't work...  any thoughts?
0
 
LVL 8

Author Comment

by:AaronReams
Comment Utility
btw, ms.Length = 0 at the byte buffer allocation line.  do i need to do something different to initialize my XmlTextWriter?
0
 
LVL 8

Author Comment

by:AaronReams
Comment Utility
Uh, I figured this out on my own courtesy of the following link...

http://www.artisticode.com/Articles/45.aspx

MemoryStream ms = new MemoryStream();
XmlTextWriter tw = new XmlTextWriter(ms, Encoding.UTF8 );
tw.Formatting = Formatting.Indented;
tw.Indentation = 4;
tw.WriteStartDocument();
tw.WriteStartElement("ClientRequest");
tw.WriteString("Test");
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string request = sr.ReadToEnd();
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Author Comment

by:AaronReams
Comment Utility
and thanks to all that looked at this question.  also thanks to thedude112286 for your suggestion.  peace.
0
 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
I think I used a StringBuilder and a StringWriter - but then you get a unicode string which is UTF16.
0
 
LVL 8

Author Comment

by:AaronReams
Comment Utility
Uh, I was able to figure this out on my own and I posted the solution above.  
0
 

Accepted Solution

by:
modulo earned 0 total points
Comment Utility
PAQed, with points refunded (250)

modulo
Community Support Moderator
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now