Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XML Sample?

Posted on 2006-06-17
5
Medium Priority
?
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: 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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

705 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