By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,166 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,166 IT Pros & Developers. It's quick & easy.

XmlTextWriter without a file

P: n/a
can anyone help me on how to create and manipulate a xmttextwriter without
having to craete a physical file.

I have an application that should return data in xml. But I do not want to
create a file and then delete it.

/Magnus
Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


Magnus wrote:
can anyone help me on how to create and manipulate a xmttextwriter without
having to craete a physical file.

I have an application that should return data in xml. But I do not want to
create a file and then delete it.


Where does the application need to return the data to? If it needs to
return the data to a stream or network connection you can open an
XmlTextWriter on a stream.
You can also open an XmlTextWriter on a MemoryStream and create the XML
in memory.
Or you can ceate an XmlTextWriter on a StringWriter and simply read out
the string at the end as in the following example:

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("gods");
xmlWriter.WriteElementString("god", "Kibo");
xmlWriter.WriteEndDocument();
xmlWriter.Close();
Console.WriteLine(stringWriter.ToString());

which writes

<?xml version="1.0" encoding="utf-16"?><gods><god>Kibo</god></gods>

to the console.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2

P: n/a
Hi. Thanks.

That was what I wanted.

One other thing: How can I change the encoding to UTF-8 or ISO-8859-1
on StringWriter the encoding property is readonly, and on XmlTextWriter I
can not set the encoding type.

Do I have to so a search and replace?

/Magnus

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...


Magnus wrote:
can anyone help me on how to create and manipulate a xmttextwriter
without having to craete a physical file.

I have an application that should return data in xml. But I do not want
to create a file and then delete it.


Where does the application need to return the data to? If it needs to
return the data to a stream or network connection you can open an
XmlTextWriter on a stream.
You can also open an XmlTextWriter on a MemoryStream and create the XML in
memory.
Or you can ceate an XmlTextWriter on a StringWriter and simply read out
the string at the end as in the following example:

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("gods");
xmlWriter.WriteElementString("god", "Kibo");
xmlWriter.WriteEndDocument();
xmlWriter.Close();
Console.WriteLine(stringWriter.ToString());

which writes

<?xml version="1.0" encoding="utf-16"?><gods><god>Kibo</god></gods>

to the console.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #3

P: n/a


Magnus wrote:
One other thing: How can I change the encoding to UTF-8 or ISO-8859-1
on StringWriter the encoding property is readonly, and on XmlTextWriter I
can not set the encoding type.


A string in .NET/C# is always UTF-16 encoded as far as I know so it
doesn't make sense to enforce another encoding if you are dealing with
strings.
If you don't want an XML declaration in that string then you can avoid
calling WriteStartDocument e.g. simply write elements as needed:

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.WriteStartElement("gods");
xmlWriter.WriteElementString("god", "Kibo");
xmlWriter.WriteEndElement();
xmlWriter.Close();
Console.WriteLine(stringWriter.ToString());

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.