How to create an XML file

  • Thread starter Thread starter Jordi Rico
  • Start date Start date
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!
 
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()
 
Thanks for all the replies!
I'll test these things tomorrow, I'll tell you if they work!
 
Back
Top