XML Writer

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
3 posts Page 1 of 1
Contributors
User avatar
Kieken72
VIP - Donator
VIP - Donator
Posts: 231
Joined: Sun May 02, 2010 10:38 am

XML Writer
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
You do not have the required permissions to view the files attached to this post.
Last edited by Kieken72 on Wed Mar 09, 2011 2:12 pm, edited 1 time in total.
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: XML Writer
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
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Kieken72
VIP - Donator
VIP - Donator
Posts: 231
Joined: Sun May 02, 2010 10:38 am

Re: XML Writer
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
3 posts Page 1 of 1
Return to “Tutorials”