473,406 Members | 2,633 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,406 software developers and data experts.

XmlTextWriter Usage question

Rob
I would like to store the contents of the xml created via the XmlTextWriter
to a string as opposed to a file. Is this possible ? If so how ? Is this
the best way to create xml that gets stored to a variable? Am I better off
using the xmldom ?
Nov 8 '06 #1
5 2124
This is possible, in fact it's even pretty easy.

Create a StringWriter that writes to a stringbuilder, or a stream based on a
memory stream. Pass either of these to the XmlTextWriter, perform your
operations, then pull your string out of the stringbuilder or the memory
stream.

It's about 3 lines of code, total...

--
Chris Mullins, MCSD.Net, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
"Rob" <ro***@yahoo.comwrote in message
news:y8******************************@comcast.com. ..
>I would like to store the contents of the xml created via the XmlTextWriter
to a string as opposed to a file. Is this possible ? If so how ? Is this
the best way to create xml that gets stored to a variable? Am I better off
using the xmldom ?

Nov 8 '06 #2
Rob
Chris,

Would it work something like...

Dim sb as New StringBuilder
Dim sw as New StringWriter(sb)
Dim tw as New XmlTextWriter(sw)

With tw
.Indentation = 4
' code to build xml. ...
.WriteEndDocumnet()
End with

tw.Flush()
tw.Close()

But now how do I view the contents of tw

MsgBox(tw) does not work
Textbox1.Text = tw does not work

Thanks,
Rob
"Chris Mullins" <cm******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
This is possible, in fact it's even pretty easy.

Create a StringWriter that writes to a stringbuilder, or a stream based on
a memory stream. Pass either of these to the XmlTextWriter, perform your
operations, then pull your string out of the stringbuilder or the memory
stream.

It's about 3 lines of code, total...

--
Chris Mullins, MCSD.Net, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
"Rob" <ro***@yahoo.comwrote in message
news:y8******************************@comcast.com. ..
>>I would like to store the contents of the xml created via the
XmlTextWriter to a string as opposed to a file. Is this possible ? If so
how ? Is this the best way to create xml that gets stored to a variable?
Am I better off using the xmldom ?


Nov 8 '06 #3
Rob wrote:
Would it work something like...

Dim sb as New StringBuilder
Dim sw as New StringWriter(sb)
Dim tw as New XmlTextWriter(sw)

With tw
.Indentation = 4
' code to build xml. ...
.WriteEndDocumnet()
End with

tw.Flush()
tw.Close()

But now how do I view the contents of tw

MsgBox(tw) does not work
Textbox1.Text = tw does not work
Even simpler

Dim sw as New StringWriter()
Dim tw as New XmlTextWriter(sw)

With tw
.Indentation = 4
' code to build xml. ...
.WriteEndDocumnet()
End with

tw.Close()

MsgBox(sw.ToString())

--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
Nov 8 '06 #4
Rob
Thanks very much !

"Oleg Tkachenko [MVP]" <so**@body.comwrote in message
news:45**************@body.com...
Rob wrote:
>Would it work something like...

Dim sb as New StringBuilder
Dim sw as New StringWriter(sb)
Dim tw as New XmlTextWriter(sw)

With tw
.Indentation = 4
' code to build xml. ...
.WriteEndDocumnet()
End with

tw.Flush()
tw.Close()

But now how do I view the contents of tw

MsgBox(tw) does not work
Textbox1.Text = tw does not work

Even simpler

Dim sw as New StringWriter()
Dim tw as New XmlTextWriter(sw)

With tw
.Indentation = 4
' code to build xml. ...
.WriteEndDocumnet()
End with

tw.Close()

MsgBox(sw.ToString())

--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net

Nov 8 '06 #5
Just change:
MsgBox(tw)
to:
MsgBox(sb.ToString())

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins

"Rob" <ro***@yahoo.comwrote in message
news:fr******************************@comcast.com. ..
Chris,

Would it work something like...

Dim sb as New StringBuilder
Dim sw as New StringWriter(sb)
Dim tw as New XmlTextWriter(sw)

With tw
.Indentation = 4
' code to build xml. ...
.WriteEndDocumnet()
End with

tw.Flush()
tw.Close()

But now how do I view the contents of tw

MsgBox(tw) does not work
Textbox1.Text = tw does not work

Thanks,
Rob
"Chris Mullins" <cm******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>This is possible, in fact it's even pretty easy.

Create a StringWriter that writes to a stringbuilder, or a stream based
on a memory stream. Pass either of these to the XmlTextWriter, perform
your operations, then pull your string out of the stringbuilder or the
memory stream.

It's about 3 lines of code, total...

--
Chris Mullins, MCSD.Net, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
"Rob" <ro***@yahoo.comwrote in message
news:y8******************************@comcast.com ...
>>>I would like to store the contents of the xml created via the
XmlTextWriter to a string as opposed to a file. Is this possible ? If
so how ? Is this the best way to create xml that gets stored to a
variable? Am I better off using the xmldom ?



Nov 9 '06 #6

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...
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...
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...
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...
5
by: Robert Dickow | last post by:
Hi, My XML files created with XmlTextWriter routines has been resulting in working files, but they include three bytes of garbage at the start of the file. The bytes are: $EF $BB $BF (I think...
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...
0
by: Martin | last post by:
Hi, I am retriving data from sql server using the xml raw clause and an xmltextwriter. this is working out fine except the xml raw clause does not put a root node on the xml that is returned...
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.