Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Querying XML in LINQ

Posted on 2014-03-06
5
Medium Priority
?
219 Views
Last Modified: 2016-02-10
I am quite new to using LINQ to query XML.

Is there any guidance in VB.Net that would better enable me to understand how to get attributes from Branch Element and to enumerate the Item elements?
0
Comment
Question by:Alyanto
[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
  • 2
5 Comments
 
LVL 1

Author Comment

by:Alyanto
ID: 39909878
Sorry to those that had already looked I did not realise initially that only images and text documents were allowable
Sample.xml
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1000 total points
ID: 39909952
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 39910605
Hi Alyanto;

The below code snippet is an example of parsing your document and accessing the data.

' Load the XML document from the file system
Dim xdoc = XDocument.Load("C:/Working Directory/Sample.xml")

' Parse the XML document into Branch and Item
Dim results = From branch In xdoc.Root.Descendants("Branch") _
              Select New With _
              { _
                  .BranchInfo = (branch.Attributes().Select(Function(a) new With _
                                { _
                                    .Branch = String.Format("{0}" & vbTab & vbTab & "{1}", a.Name, a.Value) _
                                })).ToList(), _
                  .Items = (From item In branch.Descendants("Item") _
                           select new With _
                           { _
                               .Attributes = From attrib In item.Attributes() _
                                            select new With _
                                            { _                            
                                                .Attributes = String.Format("{0}" & vbTab & vbTab & "{1}", attrib.Name, attrib.Value) _
                                            } _
                           }) _
              }

' Access the document results to create the info you need.              
For Each  branch In results 
    Console.WriteLine("Branch Information")
    For Each info In branch.BranchInfo
        Console.WriteLine(vbTab & info.Branch)
    Next
    For Each item In branch.Items
        Console.WriteLine(vbTab & "Item Information")
        For Each itemInfo In item.Attributes
            Console.WriteLine(vbTab & vbTab & itemInfo.Attributes)
        Next        
    Next
Next

Open in new window

' The results of the above code
===============================

Branch Information
  BranchNumber    2302
  BranchName    Leeds Trinity
  BranchDistrict    1301
  BranchRegion    13
  BranchZone    1
  BranchCountry    UNITED KINGDOM
  BranchCountryNo    1
  Item Information
    LongCode    05326410148
    ShortCode    10148
    ProductDescription    SG WHT STUD RET
    BranchSellingPrice    8.0000
    CurrentStockUnits    0
    ToPickUnits    0
    AnalysisCode1    99
    Analysiscode1Description    Misc - No Code
    AnalysisCode2    99
    AnalysisCode2Description    Misc - No Code
    CountryAnalysisCode    99
    CountryAnalysisCodeDescription    Default
    StockHoldingGroup    22BB
    StockHoldingGroupDescription    SUNGLASSES
  Item Information
    LongCode    05326410160
    ShortCode    10160
    ProductDescription    SG MTL HALF FRM RET
    BranchSellingPrice    8.0000
    CurrentStockUnits    0
    ToPickUnits    4
    AnalysisCode1    99
    Analysiscode1Description    Misc - No Code
    AnalysisCode2    99
    AnalysisCode2Description    Misc - No Code
    CountryAnalysisCode    99
    CountryAnalysisCodeDescription    Default
    StockHoldingGroup    22BB
    StockHoldingGroupDescription    SUNGLASSES

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39910624
Here is a link to 101 LINQ Samples | Visual Basic which show how to use all of the Linq operators for Linq to XML as well as Linq to SQL , DataSet and Objects.
0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 39911821
Thank you both.  
I have shared the points here because Fernando has provided a working example of my XML in LINQ which is a great practical starting point.  Codecruiser has provided the right level of documentation for me and this give me the means to understand and capitalise on Fernando's contribution.

/Aly
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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 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