Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

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

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
AaronReams
Asked:
AaronReams
1 Solution
 
thedude112286Commented:
That looks good, just don't forget to call ms.Close() after you get the string to free up the resources.
0
 
AaronReamsAuthor Commented:
it might look good but unfortunately it doesn't work...  any thoughts?
0
 
AaronReamsAuthor Commented:
btw, ms.Length = 0 at the byte buffer allocation line.  do i need to do something different to initialize my XmlTextWriter?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
AaronReamsAuthor Commented:
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
 
AaronReamsAuthor Commented:
and thanks to all that looked at this question.  also thanks to thedude112286 for your suggestion.  peace.
0
 
ptmcompCommented:
I think I used a StringBuilder and a StringWriter - but then you get a unicode string which is UTF16.
0
 
AaronReamsAuthor Commented:
Uh, I was able to figure this out on my own and I posted the solution above.  
0
 
moduloCommented:
PAQed, with points refunded (250)

modulo
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now