none
MemoryStream.WriteTo (Stream stream) results in truncated stream RRS feed

  • Question

  • I have an XML file that has no root node, it looks like this:

    <description name="Some name" date="Some date"\>
    <action something="thing 1"\>
    <action something="thing 2"\>
    <action something="thing 3"\>
    <!-- ... -->
    <action something="thing n"\>
    

    and I want to fix it so that it looks like this:

    <description name="Some name" date="Some date">
    <action something="thing 1"\>
    <action something="thing 2"\>
    <action something="thing 3"\>
    <!-- ... -->
    <action something="thing n"\>
    </description>
    

    There is probably an easier way but the approach that I took was to use a MemoryStream.  The code is:

    private void FixRootNode_compact (string name)
    {
        // Get the first line of the file.
        StreamReader sr = new StreamReader (name);
        string firstLine = sr.ReadLine ();
    
        // Check the line for the node terminating character '/' ...
        int loc = firstLine.IndexOf ("/>");
    
        // ... and get rid of it.
        firstLine = firstLine.Substring (0, loc) + '>';
    
        //Create a writer.
        MemoryStream ms = new MemoryStream ();
        StreamWriter sw = new StreamWriter (ms);
        sw.Flush ();   // probably not needed.
        sw.WriteLine (firstLine);
    
        // Transfer the body of the text to the writer.
        bool done = false;
        while (!done)
        {
            string line = sr.ReadLine ();
            if (line == null)
                done = true;
            else
                sw.WriteLine (line);
        }
    
        sr.Close ();
        sw.WriteLine ("</description>");
    
        // Copy the memory stream to the original file.
        StreamWriter outputFile = new StreamWriter (name, false);
        ms.WriteTo (outputFile.BaseStream);
        outputFile.Close ();
    }
    

    But the resulting file does not get all of the information.  It looks like this:

    <description name="Some name" date="Some date">
    <action something="thing 1"\>
    <action something="thing 2"\>
    <action something="thing 3"\>
    <!-- ... -->
    <action something="thing n-5"\>
    <action something="thing n-4"\>
    <action someth
    

    What am I doing wrong?  And is this the best method for modifying the contents of a file anyway.

    Dick


    Friday, June 22, 2012 5:56 PM

Answers

  • Use ReadAllLines to read the file into a string array.  Modify the array and write it using WriteAllLines.
    • Marked as answer by Dick Swager Saturday, June 23, 2012 1:36 AM
    Friday, June 22, 2012 9:01 PM

All replies

  • Use ReadAllLines to read the file into a string array.  Modify the array and write it using WriteAllLines.
    • Marked as answer by Dick Swager Saturday, June 23, 2012 1:36 AM
    Friday, June 22, 2012 9:01 PM
  • John,

    Thanks.  That works.  I'd still like to understand what the problem is with the way I initially tried to do it.

    Dick

    Saturday, June 23, 2012 1:38 AM