473,385 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XmlTextWriter without a file

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
3 5927


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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: reddy | last post by:
I am trying to insert a node into an XMLFile. using XMLTextwriter. My Question is Is it possible to do without using XMLDocument. Because its loading all the the file into memory. I just want to...
4
by: H Lee | last post by:
Hi, I'm an XML newbie, and not sure if this is the appropriate newsgroup to post my question, so feel free to suggest other newgroups where I should post this message if this is the case. I'm...
1
by: Riko Eksteen | last post by:
Hi I'm reading an xml file into an XmlDocument, adding some nodes, and writing it back out. I would like the nodes I add to assume the same level of indeting as the rest of the document. (I load...
2
by: Steve | last post by:
I'm an XML newb. I'm serializing a class and when I inspect the xml file, all the data is on one line rather than being nested and indented Is that normal? <code> StreamWriter sw = new...
0
by: Tom S. | last post by:
VS.Net ver 7.1.3088 ..Net Framework 1.1 ver 1.1.4322 SP1 When using an XmlTextWriter to write an xml document, how can I specify that the document needs to use a specific xsd file for...
8
by: Marc Gravell | last post by:
I want to write a method that will accept a stream as a parameter, and which will write xml to the stream (based in reality on database results) using the XmlTextWriter class. However, this insists...
2
by: quest | last post by:
I created xml file using XmlTextWriter. When I opened the xml file using binary editor, the first 3 bytes of the xml file consist of EF, BB, BF respectively. Is there any way I can generate the XML...
5
by: Gilgamesh | last post by:
Hello, How do I use this class to create an XML document without saving it into a file? I need to store the XML documen into a string so it could be passed as a parameter to another class....
3
by: GaryDean | last post by:
I'm using an XmlTextWriter and it's various methods such as WriteElementString, WriteStartElement, WriteEndElement, etc to create an xml document. When I instantiate the XmlTextWriter to a file......
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.