Page 1 of 1

XML Writer

Posted: Wed Mar 09, 2011 1:31 pm
by Kieken72
Hello,
I'm going to learn how to make an easy XML Writer ( It will add new nodes)
XML file
Code: Select all
<products>
  <product>
    <id>1</id>
    <name>Ipod Touch 4</name>
    <price>200</price>
    <btw>21</btw>
  </product>
  <product>
    <id>2</id>
    <name>Itunes Store Card €15</name>
    <price>15</price>
    <btw>21</btw>
  </product>
</products
Then first of all make sure u put:
Code: Select all
Imports System.XML
at the top
I added 4 labels: ID,Product name, Price and Btw , a numeric Up and Down , 2 textboxes and a combobox ( items: 0,6,12,21)

then a button and then the code of the button:)
Image

then dim the document:
Code: Select all
Dim XmlDoc As New XmlDocument
        
Then we are going to load the document:
Code: Select all
XmlDoc.load("Products.xml")
then the writing :)
Code: Select all
XmlDoc.SelectSingleNode("/products").InnerXml &= "<product><id>" & NumericUpDown1.Value & "</id><name>" & TextBox1.Text & "</name><price>" & TextBox2.Text & "</price><btw>" & ComboBox1.Text & "</btw></product>"
'selects the first node were all the product nodes are in and adds to the bottom a new node with innerxml.
        XmlDoc.Save("Products.xml")
'saves document
        MsgBox("Added", MsgBoxStyle.Information, "Info")
'show msgbox completed
        NumericUpDown1.Value = (NumericUpDown1.Value + 1)
        TextBox1.Clear()
        TextBox2.Clear()
        ComboBox1.Text = ""
'clears inputs ;)
and this is it :)


Source:
20110309-Codenstuff-XMLwriter.rar
XML Reader Tutorial

Re: XML Writer

Posted: Wed Mar 09, 2011 2:08 pm
by MrAksel
Not that I didn't understand it, but would you make the code easier to understand? Maybe put some comments? But I thought there was this XMLWriter class to do such things. Just saying.
But good job

Re: XML Writer

Posted: Wed Mar 09, 2011 2:11 pm
by Kieken72
oh give me a second :)
Code: Select all
XmlDoc.SelectSingleNode("/products").InnerXml &= "<product><id>" & NumericUpDown1.Value & "</id><name>" & TextBox1.Text & "</name><price>" & TextBox2.Text & "</price><btw>" & ComboBox1.Text & "</btw></product>"
Like the document this is a the node we will add:
Code: Select all
<product>
<id>-</id>
<name>-</name>
<price>-</price>
<btw>-</btw>
</product>
Then we will putt our values in it so:
Code: Select all
<product>
<id>" & NumericUpDown1.Value & "</id>
<name>" & TextBox1.Text & "</name>
<price>" & TextBox2.Text & "</price>
<btw>" & ComboBox1.Text & "</btw>
</product>
Then put it under the last <product></product>
Code: Select all
XmlDoc.SelectSingleNode("/products").InnerXml &= "<product><id>" & NumericUpDown1.Value & "</id><name>" & TextBox1.Text & "</name><price>" & TextBox2.Text & "</price><btw>" & ComboBox1.Text & "</btw></product>"
and then save :D
Code: Select all
XmlDoc.save