Replace a string with another string in an XML file in VB.NET

  • Thread starter Thread starter Amritha.Datta
  • Start date Start date
A

Amritha.Datta

Hi,

I need to replace a string in an XML file. For that purpose I have
writtent he below code. It is working file for a small file say about
100 lines. But It failed and went to out of memory exception if I use
a bigger file. Please let me know if there is any alternate way of
doing it. I do have a file with 3 million records.

Please help.

Dim strFile As String = "C:\temp\TextXML"
Dim result As String
Dim reader As TextReader = File.OpenText(strFile)

result = Regex.Replace(reader.ReadToEnd, "</
NewDataSet><NewDataSet>", "XYZ")
reader.Close()

FileOpen(1, strFile, OpenMode.Output, OpenAccess.Write,
OpenShare.LockWrite)

'Writes the strDocument text to the file

FileSystem.Write(1, result)

'Closes the handle to the file, allowing all programs to edit
the file

FileClose(1)

Thanks.
 
Hi,

I need to replace a string in an XML file. For that purpose I have
writtent he below code. It is working file for a small file say about
100 lines. But It failed and went to out of memory exception if I use
a bigger file. Please let me know if there is any alternate way of
doing it. I do have a file with 3 million records.

Please help.

Dim strFile As String = "C:\temp\TextXML"
Dim result As String
Dim reader As TextReader = File.OpenText(strFile)

result = Regex.Replace(reader.ReadToEnd, "</
NewDataSet><NewDataSet>", "XYZ")

What if, rather than using a RegEx, you read the file into a StringBuilder
and use the StringBuilder.Replace method?

Andrew
 
Regular expressions maybe over kill for this example, since you are not
utilizing the pattern matching that makes regex so handy. Have you tried
using a simple string replace?
 
What if, rather than using a RegEx, you read the file into a StringBuilder
and use the StringBuilder.Replace method?

Andrew

Hi Andrew.

Is there any code available for this.

I really thankful to you

Amritha
 
How big is the file? I believe your code will read in the data to one long
string, and the Regex will return another string of approximately the same
length. 3 million records doesn't indicate the file length to us.
 
How big is the file? I believe your code will read in the data to one long
string, and the Regex will return another string of approximately the same
length. 3 million records doesn't indicate the file length to us.

It is arround 200MB file
 
That's a lot to read in one swallow. The code below seems to be dependent on
the end of one element and the start of the next being on one line of text.
Given that, you could just read one line at a time, do the replacement on
that line, then write it out. You loop till the end of the file.

Another alternative is to use either an XmlReader to scan through the file.
I haven't used this much. I use XmlDocument.load(filename) to work with xml
documents. It looks like what you are trying to do is merge all the elements
with the name "NewDataSet" into a single element. You could do this within
the Xml arena rather than string arena.
 
Back
Top