Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. LINQ to XML (where clause)

LINQ to XML (where clause)

Scheduled Pinned Locked Moved LINQ
helpcsharplinqxmlquestion
3 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fabrizio Magosso
    wrote on last edited by
    #1

    Hi guys, I didn't know which forum to post this (LINQ or XML), so I'm posting it here. Anyway. I have a bit of problem with LINQ to XML here. This is my XML:

    < ResourceIndex >
    < Items Name = "List_A" >
    < Item Value = "0" > A - option 1 < / Item >
    < Item Value = "1" > A - option 2 < / Item >
    < Item Value = "2" > A - option 3 < / Item >
    < / Items >
    < Items Name = "List_B" >
    < Item Value = "0" > B - option 1 < / Item >
    < Item Value = "2" > B - option 2 < / Item >
    < / Items >
    < / ResourceIndex >

    Well, what I wanna do is select the "Item" nodes with respective attributes, but from one specific Items Name=" ? ". Here is my code:

    var q = from c in xmlDoc.Descendants("Items").Elements("Item")
    select new
    {
    Text = c.Value,
    isSelected = c.Attribute("Value").Value
    };

    This returns a list of everything, but I just want a list of . I tried to insert the where clause but I didn't succeeded because the nodes that are being listed are the bottom level and it seems I can't use where from ancestors. Can anyone help me? :confused: Thanks a lot.

    Richard DeemingR 1 Reply Last reply
    0
    • F Fabrizio Magosso

      Hi guys, I didn't know which forum to post this (LINQ or XML), so I'm posting it here. Anyway. I have a bit of problem with LINQ to XML here. This is my XML:

      < ResourceIndex >
      < Items Name = "List_A" >
      < Item Value = "0" > A - option 1 < / Item >
      < Item Value = "1" > A - option 2 < / Item >
      < Item Value = "2" > A - option 3 < / Item >
      < / Items >
      < Items Name = "List_B" >
      < Item Value = "0" > B - option 1 < / Item >
      < Item Value = "2" > B - option 2 < / Item >
      < / Items >
      < / ResourceIndex >

      Well, what I wanna do is select the "Item" nodes with respective attributes, but from one specific Items Name=" ? ". Here is my code:

      var q = from c in xmlDoc.Descendants("Items").Elements("Item")
      select new
      {
      Text = c.Value,
      isSelected = c.Attribute("Value").Value
      };

      This returns a list of everything, but I just want a list of . I tried to insert the where clause but I didn't succeeded because the nodes that are being listed are the bottom level and it seems I can't use where from ancestors. Can anyone help me? :confused: Thanks a lot.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Try this:

      var q = from i in xmlDoc.Descendants("Items")
      where i.Attribute("Name").Value == "List_A"
      from c in i.Elements("Item")
      select new
      {
      Text = c.Value,
      isSelected = c.Attribute("Value").Value
      };


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      F 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Try this:

        var q = from i in xmlDoc.Descendants("Items")
        where i.Attribute("Name").Value == "List_A"
        from c in i.Elements("Item")
        select new
        {
        Text = c.Value,
        isSelected = c.Attribute("Value").Value
        };


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        F Offline
        F Offline
        Fabrizio Magosso
        wrote on last edited by
        #3

        Thank you very much, Richard. I'd have no clue I'd have to use to 'from' clauses. Cheers. :-D

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups