Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MemoryStream vs. FileStream

Status
Not open for further replies.

nhoellei

Programmer
Feb 1, 2002
13
US
I am running into an odd situation in dealing with Memorystreams. In
my program, I create an in-memory XML document by using XMLTextWriter
pointing to a memorystream. As a quick test I dumped the stream to a
file, but the file is was truncated by 9 bytes. I skipped the
memorystream a wrote directly to a filestream object, everything
appears OK.


I instantiate the memorystream as 0 bytes, when fully loaded the stream
capacity is 131456 bytes.


Any ideas as to why the Memorystream is not including all the data? I
have also tried explicitly setting the size of the stream beyond the
potential size to no avail.


Code snippets:


MemoryStream ms = new MemoryStream(0);
ms.Seek(0,SeekOrigin.Begin);
XmlTextWriter _xwrite = new
XmlTextWriter(ms,System.Text.Encoding.UTF8);
_xwrite.Formatting = Formatting.Indented;
_xwrite.IndentChar = '\x0009';
_xwrite.WriteStartElement("ESDSubnets");
.....
_xwrite.WriteEndElement();


FileStream fs = new
FileStream(@"C:\Temp\ESDLocations.XML",FileMode.Create);


byte[] b = ms.GetBuffer();
fs.Write(b,0,ms.ToArray().Length);


ms.Flush();
_xwrite.Close();


(both these methods appears to lop off 9 bytes)
FileStream fs = new FileStream(filename,filemode.create);
ms.WriteTo(fs);


OR


FileStream fs = new FileStream(filename,filemode.create);
fs.Write(ms.getBuffer(),0,ms.ToArray().Length);
 
my first question would be, are the 9 bytes important? I mean if I lost 9 bytes when saving to file, I would only care if I could not open the xml file later on. Don't waste a bunch of time chasing a trivial issue.
 
Yes, the last 9 bytes are important. It creates a malformed XML document and saving to a file is a parameter on execution.

Here's what it leaves off:

...
<Subnet Gateway="0.0.0.0.">
<ESDServers>1</ESDServers>
<ESDSRV0>ServerNmae</ESDSRV0>
</Subnet>
</ESDSubnets>
(...Root Element...)

 
I am getting closer. Thanks for the suggestion, chiph. If I flush the filestream, it only leaves off 2 bytes. I also tried flushing the XMLTextWriter, but ended up with the same result.
 
It's possible that the XmlTextWriter is dropping some of the whitespace that wouldn't affect the Xml (excess spaces between elements, etc). Use a diff tool to compare before/after to see what's being dropped. If it's just whitespace between elements, you're fine, as that isn't needed for it to be a valid Xml document (you can have Xml all on one line if you like -- the pretty formatting just bulks up the file and makes it easy for us humans to read it).

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top