I have my class:
public class Pessoa
{
public int Id {get;set;}
public string Nome {get;set;}
}
I'm trying to serialize to XML this my class.
public static string CreateXML(object o)
{
XmlSerializer xmlSerialize = new XmlSerializer(o.GetType());
StringWriter sw = new StringWriter();
XmlTextWriter tw = new FullEndingXmlTextWritter(sw);
xmlSerialize.Serialize(tw, o);
return sw.ToString();
}
For this, I overwritten the WriteEndElement
method of the FullEndingXmlTextWritter
class that inherits from XmlTextWriter
, with the intention of generating all tags
Follows:
public class FullEndingXmlTextWritter : XmlTextWriter
{
public FullEndingXmlTextWritter(TextWriter w)
: base(w)
{
}
public FullEndingXmlTextWritter(System.IO.Stream w, System.Text.Encoding encoding)
: base(w, encoding)
{
}
public FullEndingXmlTextWritter(string fileName, System.Text.Encoding encoding)
: base(fileName, encoding)
{
}
public override void WriteEndElement()
{
base.WriteFullEndElement();
}
}
However, when I just enter the Id, for example, it does not generate the <nome>
tag too, which is what I want, just Id
I want to generate all tags according to the properties of my class and not matter if they are empty or null.