Why writing an XML string into a file is so hard now?

F

feng

OK, all I want is writting a string that represents an XML
document into a file, say C:\test.xml.

This kind of task used to be so easy with vb6. But now,
with VB.Net, it seems turned into a big deal. I tried
everything I can think of but I still can't get this dame
XML file written.

Am I missing something?

Please help!

Thanks a million.
 
F

feng

Thank you for the reply.

Well, XMLWriter was the first thing I looked at and it
doesn't seem to be helpful. Again, what I have is a string
that contains the whole XML file. I need to write this
strig into a file. What I don't want to do is to create an
XML file tag by tag, piece by piece, as the XMLWriter is
used for.

Any ideas?

Thanks again.
 
J

Jay B. Harlow [MVP - Outlook]

feng,
Why do you have a string with XML in it?

Rather than building an XML file in a string, what I would suggest, which I
believe Ken is suggesting, is you use the XMLWriter to build the XML file in
the file. This way the XMLWriter will ensure you have properly formed XML.

What it sounds like you are asking is 'I have a string, I want to write it
to a file'. Its really immaterial that the string happens to contain XML...
(until of course you find out you have poorly formed XML).

I would use the StreamWriter class to open the file, and write the string to
it.

Imports System.IO

Dim theString As String ' may contain xml

Dim writer As New StreamWriter("myfile.xml")
writer.Write(theString)
writer.Close()

Watch the optional System.Text.Encoding parameter to StreamWriter's
constructor, if you string has special characters, you may need to add the
encoding parameter to get the proper encoding.

Hope this helps
Jay
 
F

feng

Thank you Jay for your help.

First of all, I am not building an XML. I have an XML
already. That's why I don't need the XMLWriter. Secondly,
what I have, actually, is an XMLDocument object that
contains my well formated XML. So what I needed, indeed,
is to export this XML into a file -- that's all I want.

Is there a easy way to do this with VB.Net? It used to be
so easy with VB6!

Thanks
 
J

Jay B. Harlow [MVP - Outlook]

Feng,
See the other post you started, it is equally easy in .NET

Dim doc As XmlDocument
doc.Save("myfile.xml")

Note there are instances where it is handy to use an XmlWriter with a
XmlDocument, primarily to control formatting of the document saved.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top