我寫一個C#代碼來解析下面的XML文檔:解析XML文檔中的C#
<?xml version="1.0" encoding="utf-8" ?>
<teams>
<team id="1">
<name>RealMadrid</name>
<players>
<player id="1" role="Forward">
<firstname>Cristiano</firstname>
<lastname>Ronaldo</lastname>
<number>7</number>
</player>
<player id="2" role="Forward">
<firstname>Gareth</firstname>
<lastname>Bale</lastname>
<number>11</number>
</player>
<player id="3" role="Midfield">
<firstname>Toni</firstname>
<lastname>Kroos</lastname>
<number>8</number>
</player>
<player id="4" role="Midfield">
<firstname>Luka</firstname>
<lastname>Modric</lastname>
<number>19</number>
</player>
<player id="5" role="Defence">
<firstname>Sergio</firstname>
<lastname>Ramos</lastname>
<number>4</number>
</player>
<player id="6" role="Defence">
<firstname>Raphael</firstname>
<lastname>Varane</lastname>
<number>2</number>
</player>
<player id="7" role="Goalkeeper">
<firstname>Keylor</firstname>
<lastname>Navas</lastname>
<number>1</number>
</player>
</players>
</team>
<team id="2">
<name>Barcelona</name>
<players>
<player id="1" role="Forward">
<firstname>Lionel</firstname>
<lastname>Messi</lastname>
<number>10</number>
</player>
<player id="2" role="Forward">
<firstname>Neymar</firstname>
<lastname>Jr.</lastname>
<number>11</number>
</player>
<player id="3" role="Midfield">
<firstname>Ivan</firstname>
<lastname>Rakitic</lastname>
<number>4</number>
</player>
<player id="4" role="Midfield">
<firstname>Andres</firstname>
<lastname>Iniesta</lastname>
<number>8</number>
</player>
<player id="5" role="Defence">
<firstname>Gerard</firstname>
<lastname>Pique</lastname>
<number>3</number>
</player>
<player id="6" role="Defence">
<firstname>Javier</firstname>
<lastname>Mascherano</lastname>
<number>14</number>
</player>
<player id="7" role="Goalkeeper">
<firstname>Andre</firstname>
<lastname>Ter Stegen</lastname>
<number>1</number>
</player>
</players>
</team>
<team id="3">
<name>Liverpool</name>
<players>
<player id="1" role="Forward">
<firstname>Daniel</firstname>
<lastname>Sturridge</lastname>
<number>15</number>
</player>
<player id="2" role="Forward">
<firstname>Roberto</firstname>
<lastname>Firmino</lastname>
<number>11</number>
</player>
<player id="3" role="Midfield">
<firstname>Philippe</firstname>
<lastname>Coutinho</lastname>
<number>10</number>
</player>
<player id="4" role="Midfield">
<firstname>Adam</firstname>
<lastname>Lallana</lastname>
<number>20</number>
</player>
<player id="5" role="Defence">
<firstname>Joel</firstname>
<lastname>Matip</lastname>
<number>32</number>
</player>
<player id="6" role="Defence">
<firstname>Dejan</firstname>
<lastname>Lovren</lastname>
<number>6</number>
</player>
<player id="7" role="Goalkeeper">
<firstname>Simon</firstname>
<lastname>Mignolet</lastname>
<number>22</number>
</player>
</players>
</team>
</teams>
我想寫一個特定球隊的球員名字和姓氏。 我寫了下面的代碼:
var realTeam = from db in xelement.Elements("team")
where (string)db.Element("name")=="RealMadrid"
select db;
//Console.WriteLine("Real Madrid");
foreach (var e in realTeam)
{
Console.WriteLine(e);
}
這個代碼是給所有與「皇馬」的XML部分。 我應該怎樣寫這部分的名字和姓氏?
任何幫助表示讚賞!
你選擇'name'元素。嘗試'選擇db.Elements(「players」);'而不是'select db;'然後迭代玩家,每個元素都應該是類型爲'player'的姓和名。 –
@KeyurPATEL我想你的解決方案,我只得到了在控制檯下面的消息:System.Xml.Linq.XContainer + d__40 –
RoyNasr
RoyNasr所接受請註明@feal答案。如果他的解決方案爲你工作。 –