2012-07-19 69 views
0

我有這個非常簡單的代碼和一個簡單的XML文件的和平。我正在讀取每個節點並將其寫入另一個文件,並確實讓xml讀取器跳過每個備用記​​錄節點。它從下面的文件中寫入1和3個節點!任何幫助讚賞。XMLReader跳過每個備用節點

Do While (reader.Read()) 
    If (reader.NodeType = XmlNodeType.Element And (reader.LocalName = "record" Or reader.LocalName = "record1")) Then 
     writer.WriteNode(reader, True) 
     writer.Flush() 
    End If 
Loop 

-

<?xml version="1.0" encoding="UTF-8"?> 
<records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="world-check.xsd"> 
    <record> 
     <foo> 
      <bar>wtf3</bar> 
      <bar>wtf4</bar> 
     </foo> 
    </record> 
    <record> 
     <foo> 
      <bar>wtf4</bar> 
      <bar>wtf5</bar> 
     </foo> 
    </record> 
</records> 
+0

你能肯定嗎?該代碼產生該樣本中的所有記錄... – 2012-07-20 00:38:47

回答

1

請原諒我的VB,我非常純粹的C#開發人員。

XmlWriter.WriteNode()爲您編寫的節點通過EndElement節點執行XmlReader.Read(),因此當您回到While循環的開始時,您將讀取下一個Record節點。

試試這個:

Dim reader As XmlTextReader = New XmlTextReader("1.xml") 
Dim writer As XmlTextWriter = New XmlTextWriter("2.xml", Nothing) 
reader.WhitespaceHandling = WhitespaceHandling.None 

Dim reading as boolean = reader.Read() 

Do While (reading)  
    If (reader.NodeType = XmlNodeType.Element And (reader.LocalName = "record" Or reader.LocalName = "record1")) Then 
     writer.WriteNode(reader, True) 
     writer.Flush() 
    Else 
     reading = reader.Read() 
    End If 
Loop