2016-05-13 153 views
1

我已經成功地提取了所需的所有數據,但是我意識到我的數組列表並不一致,因爲並非所有玩家都具有相同的Stat。如何使用linq查詢具有相同元素和屬性名稱的XML

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

嘗試使用詞典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); 

    } 
} 
相關問題