Solved

Querying XML in LINQ

Posted on 2014-03-06
5
207 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 250 total points
ID: 39909952
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 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 63

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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