Solved

XML Sample?

Posted on 2006-06-17
5
332 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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