I don't know whether you would prefer to write the output as XML as it would be just as simpler to do so, and you can do more with the xml document that you can with text; reading and writing text documents is quite processor intensive. Anyway, this runs very fast and does not lose data. I used a 36kb text file and took less than a second to complete. The xml returns the following company's orders from Northwind outputted using FORXML:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim str As System.IO.FileStream = New System.IO.FileStream("temp.xml", IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.ReadWrite)
Dim settings As XmlWriterSettings = New XmlWriterSettings
settings.ConformanceLevel = ConformanceLevel.Fragment
settings.Indent = True
settings.IndentChars = " "
Dim reader As XmlReader = XmlReader.Create("Order_out.xml")
reader.MoveToContent()
Dim writer As XmlWriter = XmlWriter.Create("NewOrders.xml", settings)
While reader.Read()
If reader.NodeType = XmlNodeType.Element And (reader.Name = "CustomerID") Then
writer.WriteElementString(reader.Name, reader.ReadInnerXml)
reader.ReadToFollowing("CompanyName")
If reader.NodeType = XmlNodeType.Element And (reader.Name = "CompanyName") Then
writer.WriteElementString(reader.Name, reader.ReadInnerXml)
End If
End If
End While
writer.Flush()
writer.Close()
reader.Close()
End Sub
And this was the formatted output, the tags have been stripped by developerfusion;
CHOPS
Chop-suey Chinese
DROGA
Chop-suey Chinese
GELLY
Chop-suey Chinese
CHOPS
Chop-suey Chinese
JUNKS
Chop-suey Chinese
KLINE
Chop-suey Chinese
LEHAY
Chop-suey Chinese
NOJSK
Chop-suey Chinese
PLESK
Chop-suey Chinese
QUAIT
Chop-suey Chinese
RIBST
Chop-suey Chinese
RYOPS
Chop-suey Chinese
SEDUL
Chop-suey Chinese
STARC
Chop-suey Chinese
TERRY
Chop-suey Chinese
VEGRY
Chop-suey Chinese
Enter your message below
Sign in or Join us (it's free).