Solved

Querying XML in LINQ

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

Author Comment

by:Alyanto
Comment Utility
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
Comment Utility
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
 

Author Closing Comment

by:Alyanto
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

11 Experts available now in Live!

Get 1:1 Help Now