0

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
mmutilva
  • 18,688
  • 22
  • 59
  • 82

1 Answers1

2

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

serializer.Serialize(city, xmlWriter);
xmlWriter.Flush();
Sina Iravanian
  • 16,011
  • 4
  • 34
  • 45
  • Worked. Note that when using the framework's XmlSerializer there's no need to call Flush(). I would like that to be noted in YAXLib documentation. – mmutilva Jun 20 '13 at 12:04