Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Replace a string with another string in an XML file in VB.NET

Reply
Thread Tools

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

 
 
Amritha.Datta@gmail.com
Guest
Posts: n/a
 
      09-17-2007
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.

 
Reply With Quote
 
 
 
 
Jesse Houwing
Guest
Posts: n/a
 
      09-17-2007
Hello http://www.velocityreviews.com/forums/(E-Mail Removed),

> 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")
NewDataSet>>
> 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.
>


If you're replacing a fixed string, you're better off using String.Replace
instead of Regex.Replace. My guess is that that will solve your problem.

As your files get larger and larger, you might need to write a parser that
can read the contents from a buffer instead of reading the whole file all
at once, but this should suffice for the forseable future.

--
Jesse Houwing
jesse.houwing at sogeti.nl


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
how to make replace function replace globally in a string V S Rawat Javascript 5 07-03-2007 08:02 PM
A simple 'Replace' and 'Substring' question: How to add a string to another string SM Javascript 4 04-27-2007 07:22 AM
Problem to insert an XML-element by XSLT-converting from one XML-file into another XML-file jkflens XML 2 05-30-2006 09:41 AM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments