Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.NET Help Me Parse This XML

Posted on 2014-10-02
4
Medium Priority
?
426 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
  • 2
4 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 40357218
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

885 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