1

我有以下 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 值。

谢谢

4

1 回答 1

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)
于 2013-09-30T02:27:53.253 回答