Solved

XML Sample?

Posted on 2006-06-17
5
309 Views
Last Modified: 2010-04-23
Hello

I am very new to XML. What I have is a lot of products with "Attributes" which I want to store into an XMl-File

e.g:

4 Products:

Apple (Price: 1.0, weight: 50g ...)
Jewing Gum (Price: 2.0, weight: 20g ...)
Bottle of Water (Price: 3.0, weight: 10g ...)
Orange Juice (Price: 4.0, weight: 30g ...)

How can I store this into a XML-File and how can I then read these values back from the XML-FIle?

Thanks a lot!

Sven
0
Comment
Question by:Ruttensoft
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
You would create an XML file something like:

    <?xml version="1.0"?>
    <products>
        <product price="1.0" weight="50g">Apple</product>
        <product price="2.0" weight="20g">Jewing Gum</product>
        <product price="3.0" weight="10g">Bottle of Water</product>
        <product price="4.0" weight="30g">Orange Juice</product>
    </products>

You could then read it using something like:

        Dim doc As New XmlDocument
        doc.Load("C:\products.xml")

        Dim weight As String
        Dim price As String

        Dim nodes As XmlNodeList = doc.SelectNodes("products/product")
        For Each node As XmlNode In nodes

            weight = node.Attributes("weight").InnerText
            price = node.Attributes("price").InnerText

            MessageBox.Show(node.InnerText & " weighs " & weight & " and is priced at " & price)

        Next
0
 

Author Comment

by:Ruttensoft
Comment Utility
Thanks, but how can I save them?
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
How do you mean "save them" ? You mean change them in code and write them back to the file ?
0
 

Author Comment

by:Ruttensoft
Comment Utility
I mean if I have a new product, how can I add this?
And how can I change the weight of a product?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
Following on from my previous example:

To change an attribute of an existing node:

        '// Change the weight of Apple
        Dim apple As XmlNode = doc.SelectSingleNode("products/product[text()='Apple']")
        If Not apple Is Nothing Then
            apple.Attributes("weight").InnerText = "35g"
        End If

To create a new node:

        '// Add a new product
        Dim newItem As XmlNode = doc.CreateElement("product")
        newItem.InnerText = "Pineapple"

        '// Create a Weight attribute
        Dim newWeight As XmlAttribute = doc.CreateAttribute("weight")
        newWeight.InnerText = "200g"

        '// Create a Price attribute
        Dim newPrice As XmlAttribute = doc.CreateAttribute("price")
        newPrice.InnerText = "5.0"

        '// Append the attributes to the new Item
        newItem.Attributes.Append(newWeight)
        newItem.Attributes.Append(newPrice)

        '// Append the new Item to the products node
        doc.SelectSingleNode("products").AppendChild(newItem)

Saving the changes:

        '// Save the document
        doc.Save("C:\products.xml")
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.Net - 2005 Project Not Responding 24 47
Handling arrays 14 33
VB.net PaintEventArgs Handling. 10 26
Library to convert HTML to PDF. 8 42
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now