Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Serialization Size Limit in VB.Net?

I am using the following function to convert an object into XML.

Public Shared Function SerializeObject(ByVal objToSerialize As Object) As String
    Dim objXML As New Xml.Serialization.XmlSerializer(objToSerialize.GetType) 
    Dim sw As New IO.StringWriter()
    objXML.Serialize(sw, objToSerialize)
    Return sw.ToString() 
End Function

I noticed that the output file is being truncated at a certain point, which appears to be 100MB. You can see the code I use in this question: Best method for comparing XML folder data

From what I can see it looks like it is showing everything except for a few files in the last folder. I'm guessing that this is just dumb luck and that it is hitting the max length at the end of the search.

Is the 100MB cap on the XML Serialization function or is there something else at play here?

like image 480
Nicholas Post Avatar asked Aug 23 '26 22:08

Nicholas Post


1 Answers

Based on the code from the linked question, the issue is actually downstream:

Dim strObjects As String = SerializeObject(objFolder)
With New StreamWriter("Out Path")
    .Write(strObjects)
End With

You are not flushing your stream writer. The stream writer will automatically buffer a certain amount of text and then send it all at once (rather than reading a charcter, then writing a charcter, which is much less performant). There is still some text in the buffer when your code exits, but the object is disposed and garbage collected before it is written to the file. You must explicitly flush and/or close your stream before you release the object. Most people perfer to do both, but closing it is sufficient.

Try flushing the stream and the issue should be resolved:

Dim strObjects As String = SerializeObject(objFolder)
Using sr As StreamReader = New StreamReader("Out Path")
    sr.Write(strObjects)
    sr.Flush()
    sr.Close()
End Using 

(The MSDN documentation recommends using a StreamWriter from within a Using statement. I do too.)

like image 133
JDB Avatar answered Aug 26 '26 22:08

JDB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!