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

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.
 
A

Andrew Morton

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
 
G

Guest

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

Amritha.Datta

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
 
G

Guest

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.
 
A

Amritha.Datta

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top