473,464 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

xmlTextWriter

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.
Gilgamesh
Mar 31 '06 #1
5 3239
Create an XmlTextWriter through the constructor which takes in a TextWriter.
You could create a StringWriter instance of the abstract TextWriter class
and store the content written by the XmlTextWriter is that string without
ever persisting it into a file.

Thanks,
Zafar

"Gilgamesh" <gi************@aol.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
Gilgamesh

Apr 1 '06 #2
Thanks, Zafar. I got the XML document written into StringWriter. However, my
ultimate goal is to store XML document in a text field in SQL database. I
tried the StringMethod method called Write, but this method inserts \r\n at
the end of each element. Is there any way to get the XML document without
line return characters?
Thanks,
-G
"Zafar Abbas" <so*****@somewhere.com> wrote in message
news:OZ**************@TK2MSFTNGP12.phx.gbl...
Create an XmlTextWriter through the constructor which takes in a
TextWriter.
You could create a StringWriter instance of the abstract TextWriter class
and store the content written by the XmlTextWriter is that string without
ever persisting it into a file.

Thanks,
Zafar

"Gilgamesh" <gi************@aol.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
Gilgamesh


Apr 1 '06 #3
Just a correction that the method I used was ToString not Write.
-G
"Zafar Abbas" <so*****@somewhere.com> wrote in message
news:OZ**************@TK2MSFTNGP12.phx.gbl...
Create an XmlTextWriter through the constructor which takes in a
TextWriter.
You could create a StringWriter instance of the abstract TextWriter class
and store the content written by the XmlTextWriter is that string without
ever persisting it into a file.

Thanks,
Zafar

"Gilgamesh" <gi************@aol.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
Gilgamesh


Apr 1 '06 #4


Gilgamesh wrote:
I got the XML document written into StringWriter. However, my
ultimate goal is to store XML document in a text field in SQL database. I
tried the StringMethod method called Write, but this method inserts \r\n at
the end of each element. Is there any way to get the XML document without
line return characters?


That depends solely on the Formatting settings of the XmlTextWriter e.g.
you can do

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = Formatting.None;
xmlWriter.WriteStartElement("gods");
xmlWriter.WriteElementString("god", "Kibo");
xmlWriter.WriteElementString("god", "Xibo");
xmlWriter.WriteEndElement();
xmlWriter.Flush();
Console.WriteLine("XML is:\r\n{0}", stringWriter.ToString());

and the result is alike

XML is:
<gods><god>Kibo</god><god>Xibo</god></gods>
or you can set the writer to indent e.g.

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartElement("gods");
xmlWriter.WriteElementString("god", "Kibo");
xmlWriter.WriteElementString("god", "Xibo");
xmlWriter.WriteEndElement();
xmlWriter.Flush();
Console.WriteLine("XML is:\r\n{0}", stringWriter.ToString());

and the output is alike

XML is:
<gods>
<god>Kibo</god>
<god>Xibo</god>
</gods>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 1 '06 #5
Thanks, Martin. I appreciate your help.
BTW, how can I change the encoding to utf-8?
-G
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:eq**************@TK2MSFTNGP10.phx.gbl...


Gilgamesh wrote:
I got the XML document written into StringWriter. However, my ultimate
goal is to store XML document in a text field in SQL database. I tried
the StringMethod method called Write, but this method inserts \r\n at the
end of each element. Is there any way to get the XML document without
line return characters?


That depends solely on the Formatting settings of the XmlTextWriter e.g.
you can do

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = Formatting.None;
xmlWriter.WriteStartElement("gods");
xmlWriter.WriteElementString("god", "Kibo");
xmlWriter.WriteElementString("god", "Xibo");
xmlWriter.WriteEndElement();
xmlWriter.Flush();
Console.WriteLine("XML is:\r\n{0}", stringWriter.ToString());

and the result is alike

XML is:
<gods><god>Kibo</god><god>Xibo</god></gods>
or you can set the writer to indent e.g.

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartElement("gods");
xmlWriter.WriteElementString("god", "Kibo");
xmlWriter.WriteElementString("god", "Xibo");
xmlWriter.WriteEndElement();
xmlWriter.Flush();
Console.WriteLine("XML is:\r\n{0}", stringWriter.ToString());

and the output is alike

XML is:
<gods>
<god>Kibo</god>
<god>Xibo</god>
</gods>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Apr 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jain, Pranay Kumar | last post by:
Hello Everyone, I have written a simple app. that converts the dataset into excelspreadsheet. The App. uses the following architecture. First it generates the dataset with corresponding...
1
by: Koray Atsan | last post by:
Hi All I have a simple question as i am a newbie in xml. I am using XmlTextWriter to write some values to an xml file . I use XmlTextWriter textwriter = new XmlTextWriter ("path",null); to create...
3
by: Magnus | last post by:
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...
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...
3
by: K Rege | last post by:
Hi there, I tried to implement a little XML-EchoServer using Sockets and XmlTextWriter/Reader. However the server hangs while reading the input from the client (wr.Flush() seams not to work). I...
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...
4
by: Einar Høst | last post by:
Hi, I'm having weird problems using StringWriter and XmlTextWriter. My code looks like this: StringWriter sw = new StringWriter(CultureInfo.InvariantInfo); XmlTextWriter xtw = new...
4
by: quest | last post by:
Is there anyway I can generate the xml in the following format using XmlTextWriter ? Intended output: <?xml version="1.0" ?> I tried: XmlTextWriter xmlWriter = new...
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......
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.