Easiest way to build a xml fragment (write to string)

G

Guest

Hi, can anyone tell me

1) what is a easy way to build a xml fragment into a string. I don't what
the fragment to be written to file, just in a string will be fine.

I can build the xml fragment with the xmlTextWriter, but I don't want to
write to a file. But using methods like WriteStartElement,
WriteAttributeString is very handy.

Of course I can build the string with the stringbuilder but this is not a
xml-friendly and secure way. The xml fragment will be something like:

<houses>
<house houseid="1" typeid="2" />
<house houseid="2" typeid="3" />
</vakken>

2) I even don't want a "<?xml version="1.0"?>" line. How to suppress this?

Can someone advice me?
VS2005-VB.NET
Regards Coen.
 
H

Henning Krause [MVP - Exchange]

Hello,

you can use the XmlWriter and write to a String using the StringWriter. But
this will force yout encoding to UTF-16. If you need another encoding, use
the StreamWriter and write to a MemoryStream.

Best regards,
Henning Krause
 
S

Steven Cheng[MSFT]

Hi Coen,

For creating some XML fragment, you can use both XmlWriter or XMLDocument
(DOM approach). And for your request that you want to save the XML fragment
into a string object, you can simply use a StringWriter instead of a
StreamWriter so that the output of XmlWriter or XmlDocument can be held in
a string in memory instead of flush to file system.

Please feel free to post here if you have any further questions or concerns.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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