views:

77

answers:

2

Trying to serialize an XmlDocument to file. The XmlDocument is rather large; however, in the debugger I can see that the InnerXml property has all of the XML blob in it -- it's not truncated there.

Here's the code that writes my XmlDocument object to file:

// Write that string to a file.
var fileStream = new FileStream("AdditionalData.xml", FileMode.OpenOrCreate, FileAccess.Write);
xmlDocument.WriteTo(new XmlTextWriter(fileStream, Encoding.UTF8) {Formatting = Formatting.Indented});
fileStream.Close();

The file that's produced here only writes out to line like 5,760 -- it's actually truncated in the middle of a tag!

Anyone have any ideas why this would truncate here?

Update: I found the source of the issue. I was not closing the XML Text Writer before closing the file stream! D'oh!

+3  A: 

You can try to Flush the stream before closing. If AutoFlush is true, I think it gets flushed on Close() anyway, but it might be worth a shot:

// Write that string to a file. 
var fileStream = new FileStream("AdditionalData.xml", FileMode.OpenOrCreate, FileAccess.Write); 
xmlDocument.WriteTo(new XmlTextWriter(fileStream, Encoding.UTF8) {Formatting = Formatting.Indented}); 
fileStream.Flush();
fileStream.Close(); 
Paul Kearney - pk
Hey Paul, thanks for the suggestion. I had actually thought of that (sorry, should have called that out in the original post) but I got the same result. Likewise, I tried switching the encoding, thinking it might be some kind of issue there, to no avail.Any other ideas?
Brad Heller
A: 

The XmlTextWriter wasn't closed properly. Woops!

Brad Heller
Pavel Minaev