Solved

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

Posted on 2004-09-24
10
254 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export import database 4 51
Run time Error 4 47
System.Speech 2 26
Get distinct values from excel or comm seperated file 4 23
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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