2015-11-13 93 views
1

我想從MatchOdds節點中刪除所有重複的Bet元素,除了最後一個來自以下xml示例並將該文件保存在另一個目標中。Xdocument - 刪除除最後一個以外的重複元素c#

<Sports> 
    <Sport id="5" name="Tennis"> 
    <Tournament id="3" name="ATP - Basel, Switzerland"> 
     <Match id="8331188" name="Nadal, Rafael v Gasquet, Richard" 
      startDate="2015-10-31T13:30:00"> 
     <MatchOdds> 
      <Bet id="20"> 
      <Outcome Special="" name="1" odds="1.35" /> 
      <Outcome Special="" name="2" odds="3.3" /> 
      </Bet> 
      <Bet id="204"> 
      <Outcome Special="" name="1" odds="1.45" /> 
      <Outcome Special="" name="2" odds="2.85" /> 
      </Bet> 
      <Bet id="231"> 
      <Outcome Special="" name="1" odds="1.45" /> 
      <Outcome Special="" name="2" odds="2.75" /> 
      </Bet> 
      <Bet id="206"> 
      <Outcome Special="" name="2 sets" odds="1.55" /> 
      <Outcome Special="" name="3 sets" odds="2.35" /> 
      </Bet> 
      <Bet id="233"> 
      <Outcome Special="" name="2:0" odds="1.9" /> 
      <Outcome Special="" name="2:1" odds="3.5" /> 
      <Outcome Special="" name="0:2" odds="5.75" /> 
      <Outcome Special="" name="1:2" odds="6.0" /> 
      </Bet> 
      <Bet id="233"> 
      <Outcome Special="" name="2:0" odds="1.9" /> 
      <Outcome Special="" name="2:1" odds="3.5" /> 
      <Outcome Special="" name="0:2" odds="5.75" /> 
      <Outcome Special="" name="1:2" odds="6.0" /> 
      </Bet> 
      <Bet id="233"> 
      <Outcome Special="" name="2:0" odds="1.9" /> 
      <Outcome Special="" name="2:1" odds="3.5" /> 
      <Outcome Special="" name="0:2" odds="5.75" /> 
      <Outcome Special="" name="1:2" odds="6.0" /> 
      </Bet> 
      <Bet id="233"> 
      <Outcome Special="" name="2:0" odds="1.9" /> 
      <Outcome Special="" name="2:1" odds="3.5" /> 
      <Outcome Special="" name="0:2" odds="5.75" /> 
      <Outcome Special="" name="1:2" odds="6.0" /> 
      </Bet> 
      <Bet id="233"> 
      <Outcome Special="" name="2:0" odds="1.9" /> 
      <Outcome Special="" name="2:1" odds="3.5" /> 
      <Outcome Special="" name="0:2" odds="5.75" /> 
      <Outcome Special="" name="1:2" odds="6.0" /> 
      </Bet> 
      <Bet id="233"> 
      <Outcome Special="" name="2:0" odds="12312333322" /> 
      <Outcome Special="" name="2:1" odds="123123" /> 
      <Outcome Special="" name="0:2" odds="123123123" /> 
      <Outcome Special="" name="1:2" odds="123123123" /> 
      </Bet> 
     </MatchOdds> 
     </Match> 
    </Tournament> 
    </Sport> 
</Sports> 

我想使用LINQ到XMl(Xdocument類),但我不知道如何。 你能幫我解決這個問題嗎? 在此先感謝

+3

因此,您已發佈b滾動的XML:第4行10位的'Match'開始標記與'Tournament'的結束標記不匹配。第59行,第7位。 – spender

+0

我冒昧修復和重新格式化XML。 – spender

+0

謝謝!我很抱歉 –

回答

1

目前尚不清楚您的意思是相同的......在沒有更好的相同定義的情況下,我將假設所有的投注元素都是相同的。

假設你正在使用你上面引述的XML的非破碎版本:

var bets = doc.Root.Descendants("MatchOdds").Descendants("Bet").ToList(); 
var lastBet = bets.Last(); 

foreach(var bet in bets) 
{ 
    if(bet != lastBet) 
    { 
     bet.Remove(); 
    } 
} 

現在的「相同」更明智的定義可能是那些共享相同的id

我這裏選擇使用a WithoutLast extension method我隨時可供參考:

static class Ex 
{ 
    public static IEnumerable<T> WithoutLast<T>(this IEnumerable<T> source) 
    { 
     using (var e = source.GetEnumerator()) 
     { 
      if (e.MoveNext()) 
      { 
       for (var value = e.Current; e.MoveNext(); value = e.Current) 
       { 
        yield return value; 
       } 
      } 
     } 
    } 
} 

那麼你可以有這樣的可愛LINQy解決問題的辦法:

var bets = doc.Root.Descendants("MatchOdds").Descendants("Bet"); 
var betsToRemove = bets.GroupBy(b => (string)b.Attribute("id")) 
         .SelectMany(g => g.WithoutLast()) 
         .ToList(); 
betsToRemove.ForEach(b => b.Remove()); 
+0

謝謝!對不起,對於破損的XML文件,但現在我不能編輯它了! –

2

你可以嘗試通過ID分組,和稍後如果某些組有多個元素,則刪除第一個count-1元素:

var query = from el in doc.Descendants("Bet") 
       group el by el.Attribute("id").Value; 

    foreach (var g in query) 
    { 
     int count=g.Count(); 
     if (count>1) 
     { 
     g.Take(count - 1).Remove();// remove every node in the source collection from its parent node 
     } 
    } 
+0

這是一個不錯的清潔解決方案。 –

+0

謝謝@ArghyaC;) – octavioccl

+0

謝謝! :) –

相關問題