शेखर
शेखर

Reputation: 17614

linq to xml query with all attributes

I have a xml document like this

<Products>
  <Product Name="Mobile Device" Code="10000000000000000006">
    <Providers>
        <Provider Name="VODA" Code="VODA">
            <Menus>
                <Menu  Name="Home" Url="TopModelsNew.aspx" DisplayOrder="1" NewWindow="0"/>
                <Menu  Name="Top Queries" Url="TopIssues.aspx" DisplayOrder="2" NewWindow="0"/>
                <Menu  Name="Error Codes" Url="PCErrors.aspx" DisplayOrder="3" NewWindow="0"/>
            </Menus>
        </Provider>
    </Providers>
</Product>

I want to find all the menu where Product Code="10000000000000000006" and Provider Code="VODA" in a list.
The result should be like

  Name    Url            DisaplayOrder 
  Home    TopModels      0

I was trying linq to xml as follows

 XDocument xdoc = XDocument.Load(Server.MapPath("~/App_Data/LeftMenu.xml"));
        var products = from product in xdoc.Descendants("Product") where
        new{
                 .....    
            };

I am not able to find how to get the list.

Upvotes: 2

Views: 918

Answers (3)

Fung
Fung

Reputation: 3558

XDocument xdoc = XDocument.Load(Server.MapPath("~/App_Data/LeftMenu.xml"));
var menus = xdoc.Descendants("Menu").Where(x =>
    x.Ancestors("Product").First().Attribute("Code").Value == "10000000000000000006" &&
    x.Ancestors("Provider").First().Attribute("Code").Value == "VODA");

With some assumptions to the source XML document:

  • <Menu> node must and only has one <Product> ancestor node
  • <Menu> node must and only has one <Provider> ancestor node

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236318

XDocument xdoc = XDocument.Load(Server.MapPath("~/App_Data/LeftMenu.xml"));
var menus = from product in xdoc.Descendants("Product")
            where (string)product.Attribute("Code") == "10000000000000000006"
            from provider in product.Descendants("Provider")
            where (string)provider.Attribute("Code") == "VODA"
            from menu in provider.Descendants("Menu")
            select new { 
                Name = (string)menu.Attribute("Name"),
                Url = (string)menu.Attribute("Url"),
                DisplayOrder = (int)menu.Attribute("DisplayOrder")
            };

Result:

  Name            Url                  DisaplayOrder 
  "Home"          "TopModelsNew.aspx"       1
  "Top Queries"   "TopIssues.aspx"          2
  "Error Codes"   "PCErrors.aspx"           3

Upvotes: 3

MarcinJuraszek
MarcinJuraszek

Reputation: 125660

var menus = doc.Root
                .Elements("Product")
                .Where(p => (string)p.Attribute("Code") == "10000000000000000006")
                .SelectMany(p => p.Element("Providers").Elements("Provider"))
                .Where(p => (string)p.Attribute("Code") == "VODA")
                .SelectMany(p => p.Element("Menus").Elements("Menu"))
                .ToList();

It returns all 3 menu elements for your sample data. You can use projection to get anonymous object instead of XElement:

var menus = doc.Root
               .Elements("Product")
               .Where(p => (string)p.Attribute("Code") == "10000000000000000006")
               .SelectMany(p => p.Element("Providers").Elements("Provider"))
               .Where(p => (string)p.Attribute("Code") == "VODA")
               .SelectMany(p => p.Element("Menus").Elements("Menu"))
               .Select(m => new {
                   Name = (string)m.Attribute("Name"),
                   Url = (string)m.Attribute("Url"),
                   DisplayOrder = (int)m.Attribute("DisplayOrder")
               })

Upvotes: 2

Related Questions