开发者

c# linq to xml to list

开发者 https://www.devze.com 2022-12-25 21:27 出处:网络
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:

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开发者_Go百科 just get a list of strings from this?

Thanks


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".


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);


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();


I think the reason you find the "var" chaotic is your creation of the anonymous type with the "new" in your select. If you just select the one item you're after then the var will not be an anonymous type.

e.g.

select sportpage.Element("LinkPage").Element("IDList").Elements("string");

However, my preference would be to do that using the . notation like this.

List<string> ids = xDoc.Elements("SportPages").Elements("SportPage").Where(sportPage => sportPage.Attribute("type").Value == "Karate").Elements("LinkPage").Elements("IDList").Elements("string").Select(id => id.Value).ToList();


The biggest issue you were having was that you didn't grab the .Value from the returned element set. But here's another way to do it.

var ids = from sportPage in xDoc.Descendants("SportPage")
          let attrib = sportPage.Attribute("type")
          where attrib != null
          let type = attrib.Value
          where !string.IsNullOrEmpty(type)
              && type == "Karate"
          from id in sportPage.Descendants("IDList").Elements()
          select id.Value;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号