Question

I'm trying to use YAXLib to serialize an object. If I serialize directly to a string it works fine, but if I try to serialize to an XmlWriter I get an empty xml.

This is a sample class declaration (nothing weird, right?):

public class City
{
    public string Name { get; set; }
    public long Population { get; set; }
}

This is what I'm doing to serialize it:

/* Object to serialize */
var city = new City() { Name = "Montevideo", Population = 1500000 };

var serializer = new YAXSerializer(typeof(City));

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

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

/* Serialize to String */
var result2 = serializer.Serialize(city); // result2 is "<City>...</City>"

I need to use the XmlWriter approach because I want to control several aspects of the resulting xml through XmlWriterSettings (omit xml declaration, avoid indentation, control new line handling, ...).

Anyone had successfully serialized to XmlWriter using YAXLib? What am I doing wrong?

Was it helpful?

Solution

A call to xmlWriter.Flush() may solve your problem, as it did on my own testing:

serializer.Serialize(city, xmlWriter);
xmlWriter.Flush();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top