2017-06-20 67 views
0

我寫一個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部分。 我應該怎樣寫這部分的名字和姓氏?

任何幫助表示讚賞!

+0

你選擇'name'元素。嘗試'選擇db.Elements(「players」);'而不是'select db;'然後迭代玩家,每個元素都應該是類型爲'player'的姓和名。 –

+0

@KeyurPATEL我想你的解決方案,我只得到了在控制檯下面的消息:System.Xml.Linq.XContainer + d__40 – RoyNasr

+0

RoyNasr所接受請註明@feal答案。如果他的解決方案爲你工作。 –

回答

0

正如我在我的評論中提到,你不是在你的陳述中選擇players元素。請嘗試以下操作:

var realTeam = from db in xelement.Elements("team") 
       where (string)db.Element("name")=="RealMadrid" 
       select db.Elements("players"); 
//Console.WriteLine("Real Madrid"); 
if (realTeam.Count() != 1) 
{ 
    //more than 1 team called RealMadrid was found, handle this case here 
    return; 
} 
foreach (var e in realTeam.Single()) 
{ 
    Console.WriteLine("First Name: " + e.Element("firstname").Value); //or e.Element("firstname").ToString() 
    Console.WriteLine("Last Name: " + e.Element("lastname").Value); 
} 
+1

THX你的幫助,但我複製粘貼你的代碼,並用它代替我的,我在Console.WriteLine命令語句「e.Element」以下錯誤:「IEnumerable的」不包含「元素的定義'並且沒有找到接受類型''IEnumerable '的第一個參數的擴展方法'Element'。 – RoyNasr

+0

啊我想我錯過了'.Elements()',請參閱編輯。 (feal的回答有幫助) –

+0

該解決方案仍然無法編譯 –

1

做這樣的:

var xmlContent = "YOUR XML"; 
    var xmlDoc = XDocument.Parse(xmlContent); 
    var realTeamName = "RealMadrid"; 

    var realTeam = from team in xmlDoc.Elements("teams") 
            .Descendants("team") 
       where team.Element("name").Value == realTeamName 
       select team; 

    var players = realTeam.Elements("players").Elements(); // get players 
    foreach (var player in players)      // iterate over players 
    { 
    Console.WriteLine("First name: " + player.Element("firstname").Value); 
    Console.WriteLine("Last name: " + player.Element("lastname").Value); 
    } 

到目前爲止,你已經得到了球隊,但現在你需要一步進隊元素,獲得了玩家的列表,然後將單個球員名字,姓氏....

+0

當我執行此代碼時,我收到以下異常「var xmlDoc = XDocument.Parse(xmlContent);」 :「System.Xml.XmlException:'根目錄下的數據無效,第1行,第1位。'」 – RoyNasr

+0

是否用變量xmlContent填充了真正的xmlContent?因爲在這個例子中,xmlContent變量包含了「你的Xml」,這當然是無效的。 – feal

+0

是的,我寫這樣的:var xmlContent =「.. \\ .. \\ XMLFile1.xml」;其中我的XML文件名是:XMLFile1.xml – RoyNasr