Serialization to XmlWriter not working


I'm trying to serialize an object to an XmlWriter using the following YAXLib's YAXSerializer method:
var serializer = new YAXSerializer(typeof(SomeClass));

/* Serialize to XmlWriter */
var stringWriter = new StringWriter();
var xmlWriter = XmlWriter.Create(stringWriter);
serializer.Serialize(someObject, xmlWriter);

var result1 = stringWriter.ToString(); // result1 is ""

/* Serialize to String */
var restult2 = serializer.Serialize(city); // result2 is "<SomeClass>...</SomeClass>"
I also posted this same question in stackoverflow.com: here just in case.


sina_iravanian wrote Jun 19, 2013 at 11:17 PM

You need to call xmlWriter.Flush() before reading the contents of the underlying StringWriter.

mmutilva wrote Jun 20, 2013 at 12:02 PM

Calling xmlWriter.Flush() solved the issue. The thing is that you don't need to do that when serializing to an XmlWriter through the framework's XmlSerializer. However the documentation states:
Note that there are other overloads of the Serialize method that accept instances of XmlWriter and TextWriter. So far, the result is much alike that of the XmlSerializer class of the .NET Framework.