1
I have my class:
public class Pessoa
{
public int Id {get;set;}
public string Nome {get;set;}
}
I’m trying to serialize for XML this class of mine.
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 that, I wrote the method WriteEndElement
class FullEndingXmlTextWritter
who 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();
}
}
But when informing only the Id, for example, it does not generate the tag <nome>
also, which is what I want, just id
I want you to manage all tags according to the properties of my class and no matter if they are random or null.
You realize you’re changing the meaning of serialization. You’re transforming
null
in""
-– Paulo Morgado