Solved

VB.NET Help Me Parse This XML

Posted on 2014-10-02
4
420 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

632 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