简体   繁体   English

c#linq to xml to list

[英]c# linq to xml to list

I was wondering if there is a way to get a list of results into a list with linq to xml. 我想知道是否有办法将结果列表输入到linq到xml的列表中。 If I would have the following xml for example: 如果我有以下xml例如:

<?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? 我如何从IDList获取字符串列表?

I'm fairly new to linq to xml so I just tried some stuff out, I'm currently at this point: 我对linq到xml相当新,所以我只是尝试了一些东西,我现在正处于这一点:

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 谢谢

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". 给我一个两个字符串的列表,“1”和“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. 我认为你发现“var”混乱的原因是你在你的选择中创建了带有“new”的匿名类型。 If you just select the one item you're after then the var will not be an anonymous type. 如果您只选择您之后的一个项目,那么var将不是匿名类型。

eg 例如

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. 您遇到的最大问题是您没有从返回的元素集中获取.Value 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM