none
DataSet WriteXML occassionaly produces XML with 'Data at root level is invalid' RRS feed

  • Question

  • I'm persisting a dataset with the following code:

    dataSet.AcceptChanges(); 
    using (FileStream fs = CurrentLogFileInfo.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) 
    { dataSet.WriteXml(fs,System.Data.XmlWriteMode.IgnoreSchema); }

    where CurrentLogFileInfo is a System.IO.FileInfo member representing a local file. My application persists the dataset to disk every few minutes for recovery purposes, overwriting the local file with any new data.

    The issue is that on occasion the persisted data XML file will contain an extra ">" at the end of the file:

    <MyDataSet xmlns="http://mycompany/mydataset.xsd">
     ... data...
    </MyDataSet>>
    
    

    Reading the dataset back in from a file generates this error: 'Data at the root level is invalid...' because of the extra character.

    Any ideas?

     



    • Edited by 10e Wednesday, November 23, 2011 6:29 PM typo and formatting again!
    Wednesday, November 23, 2011 6:22 PM

Answers

  • I had all kinds of problems getting a clean XML file to be produced from writing to a FileStream. It always left extraneous junk at the end of the file. And if I modified the file in an editor and added some extra stuff to it, when I ran the code again I'd expect that extra stuff to get overwritten. It did if it was at the beginning of the file, but not if it was at the end of the file.

    I finally figured it out though ... you can solve the problem by opening the file with FileMode.Create instead of OpenOrCreate.  Whew, that took a lot of digging to figure that one out!!!


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by 10e Tuesday, November 29, 2011 5:03 PM
    Tuesday, November 29, 2011 5:02 PM

All replies

  • I'm just wondering why you're writing to a FileStream instead of writing directly to the disk? I bet if you didn't have that extra step, you'd not see that problem.

    dataSet.WriteXml("MyFile"); // don't need XmlWriteMode.IgnoreSchema, it's the default


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Thursday, November 24, 2011 2:47 AM
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, November 28, 2011 3:53 AM
    Moderator
  • Thanks for the quick reply.

    I'm using the FileStream so I could set the read/write permissions on the disk file. The issue with calling a subsequent DataSet.WriteXML('mydatafilepath') after the first write is that generates a UnauthorizedAccessException: Access to path 'mydatafilepath' is denied. This is because the process writing the dataset is being called from within a Windows Service installed in the local system.

     

    Any suggestions?

     


    • Edited by 10e Monday, November 28, 2011 11:45 PM
    Monday, November 28, 2011 4:55 PM
  • I had all kinds of problems getting a clean XML file to be produced from writing to a FileStream. It always left extraneous junk at the end of the file. And if I modified the file in an editor and added some extra stuff to it, when I ran the code again I'd expect that extra stuff to get overwritten. It did if it was at the beginning of the file, but not if it was at the end of the file.

    I finally figured it out though ... you can solve the problem by opening the file with FileMode.Create instead of OpenOrCreate.  Whew, that took a lot of digging to figure that one out!!!


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by 10e Tuesday, November 29, 2011 5:03 PM
    Tuesday, November 29, 2011 5:02 PM
  • Thanks!
    Tuesday, November 29, 2011 5:26 PM
  • You're welcome! Glad I could help!  =0)
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Wednesday, November 30, 2011 11:41 PM