Solved

Querying XML in LINQ

Posted on 2014-03-06
5
209 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…

752 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