如何使用linq查询具有相同元素和属性名称的XML

时间:2016-05-13 10:37:02

标签: xml linq

我已经成功提取了我需要的所有数据但我意识到我的数组列表并不一致,因为不是所有玩家都拥有相同的Stat。

XML文件:

<Team ID="3">
    <Player ID="p24">
        <Name>Kobe Bryant</Name>
        <Position>Forward-Guard</Position>
        <Stat Type="firstname">Kobe</Stat>
        <Stat Type="lastname">Bryant</Stat>
        <Stat Type="birthdate">1978-08-31</Stat>
        <Stat Type="birthplace">USA</Stat>
        <Stat Type="firstnationality">USA</Stat>
        <Stat Type="weight">212</Stat>
        <Stat Type="height">6'6</Stat>
        <Stat Type="jerseynum">24</Stat>
        <Stat Type="realposition">Forward-Gaurd</Stat>
        <Stat Type="realpositionside">Unknown</Stat>
        <Stat Type="joindate">1996-07-28</Stat>
        <Stat Type="country">USA</Stat>
    </Player>
    <Player ID="p30">
        <Name>Nick Young</Name>
        <Position>Forward-Guard</Position>
        <Stat Type="firstname">Nick</Stat>
        <Stat Type="lastname">Young</Stat>
        <Stat Type="birthdate">1985-06-01</Stat>
        <Stat Type="weight">210</Stat>
        <Stat Type="height">6'7</Stat>
        <Stat Type="jerseynum">30</Stat>
        <Stat Type="realposition">Forward-Guard</Stat>
        <Stat Type="realpositionside">Unknown</Stat>
        <Stat Type="joindate">2015-07-02</Stat>
        <Stat Type="country">USA</Stat>
    </Player>
    <TID>ARS</TID>
    <Stadium ID="350">
        <Capacity>19000</Capacity>
        <Name>Staples Center</Name>
    </Stadium>
    <TeamOfficial Type="Assistant Manager" ID="AM56" country="USA">
        <PersonName>
            <BirthDate>1975-11-16</BirthDate>
            <First>Mark</First>
            <Last>Madsen</Last>
            <join_date>2014-07-01</join_date>
        </PersonName>
    </TeamOfficial>
    <TeamOfficial Type="Assistant Coach" ID="AC51" country="USA">
        <PersonName>
            <BirthDate>1968-10-22</BirthDate>
            <First>Jim</First>
            <Last>Eyen</Last>
            <join_date>1999-09-30</join_date>
        </PersonName>
    </TeamOfficial>
</Team>

C#代码:

XDocument xDoc = XDocument.Load("test.xml");

var TeamQ = from T in xDoc.Descendants("Team")
            where (string)T.Attribute("ID") == "3"
            from P in T.Elements("Player")
            let fn = P.Elements("Stat")
            select new
            {
                PlayerTeamID = (string)P.Attribute("ID"),
                Position = (string)P.Element("Position"),
                Stats = fn.Select(x => (string)x.Value)
                .ToList()

            };

            foreach (var tP in TeamQ)
            {
                listBox.Items.Add(tP.PlayerTeamID);              
            }

我想将这些数据存储在对象列表中。但是当我尝试将值存储在类属性中时,由于缺少某些元素,因此数据不一致。例如,如果我试图在第二个玩家到达第二个玩家时将第一个国籍存储在课堂中,它将存储第二个玩家的高度。

1 个答案:

答案 0 :(得分:1)

尝试使用词典stats而不是列表:

XDocument xDoc = XDocument.Load(@"c:\test.xml");
var TeamQ = from T in xDoc.Descendants("Team")
            where (string)T.Attribute("ID") == "3"
            from P in T.Elements("Player")
            let fn = P.Elements("Stat")
            select new
            {
                PlayerTeamID = (string)P.Attribute("ID"),
                Position = (string)P.Element("Position"),
                Stats = fn.Select(x => 
                                  new { key = x.Attribute("Type").Value
                                                                 .ToString(), 
                                        val = x.Value.ToString() })
                          .ToDictionary(k => k.key, 
                                        var => var.val, 
                                        StringComparer.OrdinalIgnoreCase)
            };

foreach (var tP in TeamQ)
{
    Console.WriteLine(tP.PlayerTeamID);
    //example how to get a value for a key
    if (tP.Stats.ContainsKey("firstnationality"))
    {
       Console.WriteLine(tP.Stats["firstnationality"]);  
    }
    else
    {
        Console.WriteLine("\"firstnationality\" not found");  
    }

    //example output all key->values pairs 
    foreach (var st in tP.Stats)
    {
        Console.WriteLine("{0}=>{1}",st.Key,st.Value);

    }
}