How to append a start tag and end tag to a big file RRS feed

  • Question

  • Hi All,

    I have a log file with a size of 100+MB in the below format and before loading this file to an xml document i have to add a start tag and end tag to the below content <root></root> and have to load in runtime.





    Can somebody help me in how to do this

    • Edited by Avatar 123 Tuesday, June 30, 2020 11:30 AM
    Tuesday, June 30, 2020 11:28 AM

All replies

  • You could pre-process the file by reading in the file via File.ReadAllLines(filename).ToList() then use List.Insert to add the start/end tag, save the file then do the normal processing by reading the file in again.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Tuesday, June 30, 2020 12:26 PM
  • Surprisingly there is no easy way to inject content into the middle of a text file in .NET. You can probably find a hack or perhaps call into Win32 to do it but if you want a (reasonably fast) solution that doesn't load the entire file into memory at once then stream the file through and attach the content. 

    static void AppendXml ( string filename )
        var temp = Path.ChangeExtension(filename, ".tmp");
        using (var output = File.OpenWrite(temp))
        using (var input = File.OpenRead(filename))
            //Prepend - could also use a StreamWriter...
            WriteString(output, "<root>");
            WriteString(output, "</root>");
        //Swap files
        File.Move(temp, filename);
    static void WriteString ( Stream stream, string value )
        var data = Encoding.UTF8.GetBytes(value);
        stream.Write(data, 0, data.Length);

    Other approaches that don't work in .NET:

    - Opening the stream and using Write (it will stomp over whatever is there)
    - Using StreamWriter (always appends)

    Michael Taylor

    • Proposed as answer by Naomi NModerator Tuesday, June 30, 2020 8:57 PM
    • Unproposed as answer by Avatar 123 Wednesday, July 1, 2020 5:41 AM
    Tuesday, June 30, 2020 1:38 PM
  • Surprisingly there is no easy way to inject content into the middle of a text file in .NET.
    > You can probably find a hack or perhaps call into Win32

    No.  This is not a language thing, it's an operating system thing.  You can't insert or delete bytes in the middle of a file without copying all the rest of the bytes in the file.  It will have to be done a line at a time, writing the results to a new file.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, July 1, 2020 3:26 AM
  • Hi Karen Payne,

    Thanks for your response.

    I tried using File.ReadAllLines(filename).ToList(), but my file is a live log and when i try to use your suggestion it will throw the below error

    "Error occurred while opening log file The process cannot access the file 'D:\txt.log' because it is being used by another process."

    Is there any other way to read the file with read permissions?

    Wednesday, July 1, 2020 5:46 AM
  • For big files, you can use Memory-Mapped Files
    Wednesday, July 1, 2020 7:15 AM
  • Hi

    var filetext = File.ReadAllText(filename)
    var alltext = "<root>" + filetext + "</root>"
    File.WriteAllText("file path",alltext)

    Thanks and Regards

    Laxmidhar sahoo

    Wednesday, July 1, 2020 4:43 PM