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

Discussion in 'ASP .Net' started by Amritha.Datta@gmail.com, Sep 17, 2007.

  1. Guest

    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.
     
    , Sep 17, 2007
    #1
    1. Advertising

  2. Hello ,

    > 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
     
    Jesse Houwing, Sep 17, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jkflens
    Replies:
    2
    Views:
    1,546
    jkflens
    May 30, 2006
  2. Alun
    Replies:
    3
    Views:
    4,655
    Masudur
    Feb 18, 2008
  3. Prasad S
    Replies:
    2
    Views:
    259
    Dr John Stockton
    Aug 27, 2004
  4. SM
    Replies:
    4
    Views:
    228
  5. V S Rawat
    Replies:
    5
    Views:
    345
    Richard Cornford
    Jul 3, 2007
Loading...

Share This Page