Representando valores nulos de manera diferente al serializar objetos a XML
Estoy serializando objetos a XML como así usando el siguiente código:
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MyClass thisClass = new MyClass() { One = "Foo", Two = string.Empty, Three = "Bar" };
Serialize<MyClass>(thisClass, @"C:\Users\JMK\Desktop\x.xml");
}
static void Serialize<T>(T x, string fileName)
{
XmlSerializer v = new XmlSerializer(typeof(T));
TextWriter f = new StreamWriter(fileName);
v.Serialize(f, x);
f.Close();
}
}
public class MyClass
{
public string One { get; set; }
public string Two { get; set; }
public string Three { get; set; }
}
}
Esto resulta en el siguiente XML:
<?xml version="1.0" encoding="utf-8"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<One>Foo</One>
<Two />
<Three>Bar</Three>
</MyClass>
Todo esto está muy bien, aparte de una cosa. Si uno de mis valores es nulo, no puedo omitir esto del XML, debe estar allí y no puedo representarlo como<Two />
, en cambio necesito representar esto como<Two></Two>
.
¿Es esto posible usando mi método actual?