473,569 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2130
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.co mwrote 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(s w)

With tw
.Indentation = 4
' code to build xml. ...
.WriteEndDocumn et()
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******** ********@TK2MSF TNGP04.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.co mwrote in message
news:y8******** *************** *******@comcast .com...
>>I would like to store the contents of the xml created via the
XmlTextWrit er 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(s w)

With tw
.Indentation = 4
' code to build xml. ...
.WriteEndDocumn et()
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(s w)

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

tw.Close()

MsgBox(sw.ToStr ing())

--
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.comw rote 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(s w)

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

tw.Flush()
tw.Close()

But now how do I view the contents of tw

MsgBox(tw) does not work
Textbox1.Tex t = tw does not work

Even simpler

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

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

tw.Close()

MsgBox(sw.ToStr ing())

--
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.ToStr ing())

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

"Rob" <ro***@yahoo.co mwrote 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(s w)

With tw
.Indentation = 4
' code to build xml. ...
.WriteEndDocumn et()
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******** ********@TK2MSF TNGP04.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.co mwrote in message
news:y8******* *************** ********@comcas t.com...
>>>I would like to store the contents of the xml created via the
XmlTextWrite r 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
6141
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 insert in the front. My code is give below. Is it possible to do without using XMLDOcument? Dim masterDoc As String =...
5
9147
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 datatables(each datatable represents a worksheet).It uses an Xslt file to do the transformation of the xml (generate from dataset) and write the output xml...
1
1875
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 the xml doc. I want to append some values to the doc when next time my main form loads So I use same statement in the Form_Load XmlTextWriter...
4
11496
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 having issues using XmlTextWriter, saving it out to a file with UTF8 encoding, and seeing "dirty", or "human unreadable" characters show up *right...
4
9280
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 XmlTextWriter(sw); // NullReferenceException thrown here! xtw.WriteStartDocument(); xtw.WriteStartElement("MyRoot");
5
4984
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 it always outputs these) I am using VS 2002 and C#.
0
1003
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 validation? I've tried the following, but when I load the xml document in IE, nothing displays:
0
1089
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 from sql server and subsequently an xmldocument can't be created from the xmltextwriter as there is no root element. however if the xml raw query...
8
10412
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 on closing my stream, and I can't convince it not to. A much-simplified example is below; basically, as soon as the writer is disposed (marked...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5501
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.