Solved

XML Sample?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

864 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

24 Experts available now in Live!

Get 1:1 Help Now