循环遍历XML并删除每个第10个节点实例 - 如何?

时间:2013-09-24 00:05:45

标签: .net xml

我有以下大小的XML文件 -

<?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>

这只是我需要迭代并删除'TenDayData'的每个第十个节点实例的这些段的堆(250个左右)中的一个。每组只有十个'TenDayData'实例,所以我每次都要删除最后一个。

最好的方法是什么? 我熟悉XmlDocument和XmlTextReader中的dot net childNodes,但是无法弄清楚如何在不使用ForNext循环的情况下区分10个子节点,以及听起来不是正确方法的计数器。

任何线索

我使用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 个答案:

答案 0 :(得分: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)