2013-09-24 72 views
1

我有一個XML的以下文件級塊通過XML循環並刪除每個第10個節點實例 - 如何?

<?xml version="1.0" encoding="utf-8"?> 
<TenDayWeather> 
    <TenDay ID="B3_7187"> 
    <TenDayData> 
     <Day>FRI 9</Day> 
     <MinTemp>4°C</MinTemp> 
     <MaxTemp>10°C</MaxTemp> 
     <Icon>Rain</Icon> 
     <IsWeekday>true</IsWeekday> 
    </TenDayData> 
    <TenDayData> 
     <Day>SAT 10</Day> 
     <MinTemp>6°C</MinTemp> 
     <MaxTemp>15°C</MaxTemp> 
     <Icon>Fewshowers</Icon> 
     <IsWeekday>false</IsWeekday> 
    </TenDayData> 
    <TenDayData> 
     <Day>SUN 11</Day> 
     <MinTemp>4°C</MinTemp> 
     <MaxTemp>15°C</MaxTemp> 
     <Icon>Fewshowers</Icon> 
     <IsWeekday>false</IsWeekday> 
    </TenDayData> 
    <TenDayData> 
     <Day>MON 12</Day> 
     <MinTemp>5°C</MinTemp> 
     <MaxTemp>14°C</MaxTemp> 
     <Icon>Rain</Icon> 
     <IsWeekday>true</IsWeekday> 
    </TenDayData> 
    <TenDayData> 
     <Day>TUE 13</Day> 
     <MinTemp>2°C</MinTemp> 
     <MaxTemp>13°C</MaxTemp> 
     <Icon>Fewshowers</Icon> 
     <IsWeekday>true</IsWeekday> 
    </TenDayData> 
    <TenDayData> 
     <Day>WED 14</Day> 
     <MinTemp>3°C</MinTemp> 
     <MaxTemp>14°C</MaxTemp> 
     <Icon>Fewshowers</Icon> 
     <IsWeekday>true</IsWeekday> 
    </TenDayData> 
    <TenDayData> 
     <Day>THU 15</Day> 
     <MinTemp>3°C</MinTemp> 
     <MaxTemp>16°C</MaxTemp> 
     <Icon>Fine</Icon> 
     <IsWeekday>true</IsWeekday> 
    </TenDayData> 
    <TenDayData> 
     <Day>FRI 16</Day> 
     <MinTemp>5°C</MinTemp> 
     <MaxTemp>16°C</MaxTemp> 
     <Icon>Cloudy</Icon> 
     <IsWeekday>true</IsWeekday> 
    </TenDayData> 
    <TenDayData> 
     <Day>SAT 17</Day> 
     <MinTemp>8°C</MinTemp> 
     <MaxTemp>16°C</MaxTemp> 
     <Icon>Rain</Icon> 
     <IsWeekday>false</IsWeekday> 
    </TenDayData> 
    <TenDayData> 
     <Day>SUN 18</Day> 
     <MinTemp>4°C</MinTemp> 
     <MaxTemp>13°C</MaxTemp> 
     <Icon>Showers</Icon> 
     <IsWeekday>false</IsWeekday> 
    </TenDayData> 
    </TenDay> 
    <TenDay ID="B3_11194"> 
    <TenDayData> 

etc etc, repeats some 250 times with a different TenDay ID 


    </TenDay> 
</TenDayWeather> 

這僅僅是一個堆的一個(250左右)的這些段,我需要遍歷和刪除TenDayData的」每第十節點實例的」。每個組中只有10個'TenDayData'實例,所以我需要每次刪除最後一個。

這樣做的最好方法是什麼? 我對XmlDocument和XmlTextReader中的dot net childNodes很熟悉,但無法弄清楚如何區分10個子節點而不使用ForNext循環和一個聽起來不像正確方法的計數器。

任何線索任何人

我使用vb.net,但可以讀取大多數c#。

順便提一下,這是我正在玩的代碼。

  Dim reader As New XmlTextReader(fSourceXmlPath & xmlFileName) 
      Dim doc As XmlDocument = New XmlDocument() 
      doc.Load(reader) 
      reader.Close() 

      Dim childNode As XmlNode 
      Dim nodeList As XmlNodeList 
      Dim iNode As Integer = 0 

      nodeList = doc.GetElementsByTagName("//TenDayWeather/TenDay[@ID='B3_7187']") 
      Console.Write(nodeList.Count.ToString) 
      For Each node As XmlElement In nodeList 
       iNode += 1 
       If iNode = 10 Then 
        Console.WriteLine(node("TenDayData").InnerText) 
        childNode = doc.SelectSingleNode("//TenDayWeather/TenDay[@ID='B3_7187']/TenDayData") 
        childNode.ParentNode.RemoveChild(childNode) 
       End If 
      Next 

      doc.Save(fSourceXmlPath & xmlFileName) 

不幸的是,從來沒有任何nodeList.count值。

感謝

回答

1

無法得到解決的LINQ(感謝scartag)頭,因爲我已經有一堆的XmlDocument代碼到位,我堅持了下來。 這就是我最終決定的 - 也許它會幫助別人。

  Dim iNode As Integer 
      For Each xn As XmlNode In doc.SelectNodes("//TenDayWeather/TenDay") 
       For Each childNode As XmlNode In xn.SelectNodes("TenDayData") 
        If childNode IsNot Nothing Then 
         iNode += 1 
         If iNode = 10 Then 
          childNode.ParentNode.RemoveChild(childNode) 
          iNode = 0 
         End If 
        End If 
       Next 
      Next 
      Console.Write("Deleted tenth day data." + vbNewLine)