Saving an XML

I cant figure out how to save an xml file the right way. Say i want TextBox1 to save in node 1 and TextBox2 to save in node 2. So could someone please help.


Answer this question

Saving an XML

  • jptorres

    Thanks alot

  • paulfriday

    Good news -- the XmlTextWriter (look for it in the online help or Google it) will help you do this in no time.

    Of course, exact usage depends a lot on how you want to structure your XML. A simple example:

    Using xtw As New Xml.XmlTextWriter("c:\tmp\test.xml", System.Text.Encoding.UTF8)

    xtw.WriteStartElement("MyDocumentRoot")
    xtw.WriteAttributeString(
    "Text1", "Text1.Text goes here"
    )
    xtw.WriteAttributeString(
    "Text2", "Text2.Text goes here"
    )
    xtw.WriteEndElement()
    xtw.Close()

    End Using

    This will create two attributes, Text1 and Text2 on the root node of your XML document. Of course, in real life you'll most likely want to have a separate node for each text box; that's very easy as well:

    Using xtw As New Xml.XmlTextWriter("c:\tmp\test2.xml", System.Text.Encoding.UTF8)

    xtw.WriteStartElement("MyDocumentRoot")
    xtw.WriteStartElement(
    "Text1"
    )
    xtw.WriteAttributeString(
    "Text", "Text1.Text goes here"
    )
    xtw.WriteEndElement()
    xtw.WriteStartElement(
    "Text2"
    )
    xtw.WriteAttributeString(
    "Text", "Text1.Text goes here"
    )
    xtw.WriteEndElement()
    xtw.WriteEndElement()
    xtw.Close()

    End Using

    To read back your XML, there's the XmlTextReader, along with many other goodies in the XML namespace.

    '//mdb 


  • Saving an XML