How to create an XML file

J

Jordi Rico

Hi, this is something I've been trying and I'm not capable to do.
I'm able to read from any XML file with .Net objects like XMLDoc,
XMLNode, etc, with no problem.
But when I want to create an XML file, I don't know how to use these
objects (or the ones supposed to do this), so I have to use
StreamWriter in this way:

oSW.Write("<tag value=""1"">")

I'm sure there's a best way to do this, but I haven't found yet.
I'd like some help with an example of code to write to a file an XML as
simple like this one:

<RootNode>
<tag value="1"/>
<tag value="2">
<subtag value="33" id="p"/>
</tag>
</RootNode>

Thanks in advance!
 
Z

zacks

Use the XmlTextWriter class.

Note: THE FOLLOWING CODE IS UNTESTED!!

dim twr as XmlTextWriter

twr = new XmlTextWriter(PathToWrite, Nothing)
twr.Formatting = System.Xml.Formatting.Indented
twr.WriteStartDocument(True)
twr.WriteStartElement("RootNode")
twr.WriteStartElement("tag")
twr.WriteAttributeString("value", "1)
twr.WriteEndElement
twr.WriteStartElement("tag")
twr.WriteAttributeString("value", "2")
twr.WriteStartElement("subtag")
twr.WriteAttributeString("value", "33")
twr.WriteAttributeString("id", "p")
twr.WriteEndElement()
twr.WriteEndElement()
twr.WriteEndElement()
twr.Flush()
twr.Close()
 
J

Jordi Rico

Thanks for all the replies!
I'll test these things tomorrow, I'll tell you if they work!
 

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