Solved

VB.NET Help Me Parse This XML

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

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 62

Expert Comment

by:Fernando Soto
ID: 40357594
Not a problem, glad to help.
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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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: …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

919 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

18 Experts available now in Live!

Get 1:1 Help Now