Solved

XML Sample?

Posted on 2006-06-17
5
339 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
ID: 16926732
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
ID: 16926798
Thanks, but how can I save them?
0
 
LVL 52

Expert Comment

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

Author Comment

by:Ruttensoft
ID: 16926856
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
ID: 16926888
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

740 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