Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Querying XML in LINQ

Posted on 2014-03-06
5
Medium Priority
?
225 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
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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 When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Loops Section Overview

926 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