Solved

VB.NET Help Me Parse This XML

Posted on 2014-10-02
4
418 Views
Last Modified: 2014-10-02
I need to parse this xml and set each node value to a variable.

<output xmlns="http://www.upcdatabase.org/">
<valid>true</valid>
<number>0111222333446</number>
<itemname>UPC Database Testing Code</itemname>
<alias>Testing Code</alias>
<description>http://upcdatabase.org/code/0111222333446</description>
<avgprice>123.45</avgprice>
<rate_up>6</rate_up>
<rate_down>0</rate_down>
</output>

Open in new window


 Dim upcValid As Boolean = 
 Dim upcNumber As String = 
 Dim upcItemname As String = 
 Dim upcAlias As String = 
 Dim upcDescription As String = 
 Dim upcAvgPrice As Decimal = 

Open in new window

0
Comment
Question by:FamousMortimer
[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
  • 2
4 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 40357218
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40357445
Hi FamousMortimer;

This code snippet should do what you need.

'' Load the XML into memory. Change the path and file name to what you need
Dim xdoc As XDocument = XDocument.Load("C:\Working Directory\output.xml")
'' If the XML is already in memory as a string then use this line to load it 
'' to an XML document.
Dim xdoc As XDocument = XDocument.Parse(XmlVariableNameHere)
'' Get a reference to the xml name space
Dim ns As XNamespace  = xdoc.Root.Name.Namespace

'' Query the XML document to parse the values. The results of the query will 
'' be placed into a class called OutputData, you can modify to your needs
Dim result  As OutputData = (From o in xdoc.Descendants(ns + "output") _
                             Select New OutputData With { _
                                .upcValid = If(o.Element(ns + "valid").Value = "true", True, False), _
                                .upcNumber = o.Element(ns + "number").Value, _
                                .upcItemname = o.Element(ns + "itemname").Value, _
                                .upcAlias = o.Element(ns + "alias").Value, _
                                .upcDescription = o.Element(ns + "description").Value, _
                                .upcAvgPrice = Decimal.Parse(o.Element(ns + "avgprice").Value) _
                             }).FirstOrDefault()

    '' Test to see if you have result
    If result IsNot Nothing Then                  
        '' Process result here
        '' result.upcValid
        '' ....
        '' result.upcAvgPrice                             
    End If                                        

'' Class to hold the results
Public Class OutputData
    Public Property upcValid As Boolean 
    Public Property upcNumber As String 
    Public Property upcItemname As String
    Public Property upcAlias As String
    Public Property upcDescription As String
    Public Property upcAvgPrice As Decimal 
End Class

Open in new window

0
 
LVL 10

Author Closing Comment

by:FamousMortimer
ID: 40357495
Thank you very much.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40357594
Not a problem, glad to help.
0

Featured Post

Industry Leaders: 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

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.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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