c# linq to xml to list

12,139

Solution 1

This query works - tested and verified:

var ID2 = (from sportpage in xDoc.Descendants("SportPages").Descendants("SportPage")
           where sportpage.Attribute("type").Value == "Karate"
           select sportpage)
          .Descendants("LinkPage")
          .Descendants("IDList")
          .Elements("string")
          .Select(d => d.Value)
          .ToList();

Gives me a list of two strings, "1" and "2".

Solution 2

var myStrings = xDoc.Descendants("SportPage")
                    .Where(d => d.Attribute("type").Value == "Karate")
                    .Descendants("IDList")
                    .Descendants("string")
                    .Select(d => d.Value);

to see your string:

xDoc.Descendants("SportPage")
    .Descendants("IDList")
    .Where(d => d.Attribute("type").Value == "Karate")
    .Descendants("string")
    .Select(d => d.Value)
    .ToList()
    .ForEach(Console.WriteLine);

Solution 3

Do you mean this?

List<string> IDs = xDoc.Descendants("SportPages").Descendants("SportPage")
    .Where( anySportPage => anySportpage.Attribute("type").Value == "Karate" )
    .Select( karateSportPage => karateSportpage.Element("LinkPage").Element("IDList").Elements("string"))
    .ToList();
Share:
12,139
WtFudgE
Author by

WtFudgE

Updated on July 16, 2022

Comments

  • WtFudgE
    WtFudgE over 1 year

    I was wondering if there is a way to get a list of results into a list with linq to xml. If I would have the following xml for example:

    <?xml version="1.0"?>
    <Sports xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <SportPages>
            <SportPage type="test">
                <LinkPage>
                    <IDList>
                        <string>1</string>
                        <string>2</string>
                    </IDList>
                </LinkPage>
            </SportPage>
        </SportPages>
    </Sports>
    

    How could I get a list of strings from the IDList?

    I'm fairly new to linq to xml so I just tried some stuff out, I'm currently at this point:

    var IDs = from sportpage in xDoc.Descendants("SportPages").Descendants("SportPage")
                          where sportpage.Attribute("type").Value == "Karate"
                          select new
                          {
                              ID = sportpage.Element("LinkPage").Element("IDList").Elements("string")
                          };
    

    But the var is to chaotic to read decently. Isn't there a way I could just get a list of strings from this?

    Thanks

  • marc_s
    marc_s almost 14 years
    but you're neglecting the condition sportpage.Attribute("type").Value == "Karate"
  • marc_s
    marc_s almost 14 years
    I'm getting: "Error: Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.‌​IEnumerable<System.X‌​ml.Linq.XElement>>' to 'System.Collections.Generic.List<string>'"
  • marc_s
    marc_s almost 14 years
    Also you need to be more careful with your upper- and lower-case: e.g. this statement doesn't work: .Where( anySportPage => anySportpage. - you need to use the same casing in both cases!! anySportPage vs. anySportpage - not identical!
  • Raquel
    Raquel almost 14 years
    This will throw a object null exception if you ever have a SportPage element that doesn't have a type attribute.
  • Raquel
    Raquel almost 14 years
    This way is rather lazy. If you have an explicit schema you can use the same thing you have in your example in the question. You just need to add a .Value to the end of the statement in your select.
  • WtFudgE
    WtFudgE almost 14 years
    solved it, I removed the LinkPage descendants and SportPages descendants because thanks to other replies I figured out it autosearches :) So thanks a lot all of u!
  • ANeves
    ANeves almost 14 years
    Born out of not testing and assuming I knew what came out of the .Elements(). (Using var IDs would work, but I never really liked var.) Thanks for the corrections, and for writing down a correct answer. :)