?
Solved

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

Posted on 2004-09-24
10
Medium Priority
?
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 4

Expert Comment

by:thedude112286
ID: 12146531
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
ID: 12147028
it might look good but unfortunately it doesn't work...  any thoughts?
0
 
LVL 8

Author Comment

by:AaronReams
ID: 12147044
btw, ms.Length = 0 at the byte buffer allocation line.  do i need to do something different to initialize my XmlTextWriter?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Author Comment

by:AaronReams
ID: 12147440
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
 
LVL 8

Author Comment

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

Expert Comment

by:ptmcomp
ID: 12152329
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
ID: 12402077
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
ID: 12437025
PAQed, with points refunded (250)

modulo
Community Support Moderator
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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!
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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