Solved

VB.NET Help Me Parse This XML

Posted on 2014-10-02
4
407 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
Comment Utility
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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
Comment Utility
Thank you very much.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, glad to help.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

16 Experts available now in Live!

Get 1:1 Help Now