Override XmlTextWriter - Serialize Class to XML absolutely all attributes

1

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.

    
asked by anonymous 11.03.2014 / 21:00

2 answers

1

Try to enforce a data contract in your class, explaining that the column may be null:

[DataContract]
public class Pessoa 
{
    [DataMember]
    public int Id {get;set;}
    [DataMember]
    [XmlElement(IsNullable = true)]
    public string Nome {get;set;}
}
    
11.03.2014 / 21:09
1

Keeping in mind the @CiganoMorrisonMendez alert (which <Nome xsi:nil="true"><Nome> is different from <Nome></Nome> ), if that's what you really want (assume that the null string is equivalent to the empty string), then you can change the definition of its Pessoa class for this equivalence to be explicit, using the implementation of the Nome property so that it always returns a non-null value, as shown in the code below.

class Program
{
    static void Main(string[] args)
    {
        Pessoa p = new Pessoa { Id = 123 };
        Console.WriteLine(CreateXML(p));
    }

    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();
    }

    public class FullEndingXmlTextWritter : XmlTextWriter
    {
        public FullEndingXmlTextWritter(TextWriter w)
            : base(w)
        {
        }

        public override void WriteEndElement()
        {
            base.WriteFullEndElement();
        }
    }
}

public class Pessoa
{
    private string nome;

    public int Id { get; set; }

    public string Nome
    {
        get { return nome ?? ""; }
        set { this.nome = value; }
    }
}
    
17.04.2014 / 20:32