Override Xmltextwriter - Serialize Class for XML absolutely all attributes

Asked

Viewed 368 times

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 ""-

2 answers

1

having in mind the alert of @Ciganomorrisonmendez (which <Nome xsi:nil="true"><Nome> is different from <Nome></Nome>), if this is really what you want (assume that the null string is equivalent to the empty string), then you can change the definition of your class Pessoa so that this equivalence is explicit, using the implementation of the property Nome 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; }
    }
}

1

Try forcing a data contract into your class, stating that the column can be null:

[DataContract]
public class Pessoa 
{
    [DataMember]
    public int Id {get;set;}
    [DataMember]
    [XmlElement(IsNullable = true)]
    public string Nome {get;set;}
}
  • Unfortunately it didn’t work out

  • @Rod I updated the answer, now predicting that strings can be null.

  • When using IsNullable = True, he gets like this: <Nome xsi:nil="true"> </Nome> And I’d like it to stay that way: <Nome></Nome> , So I tried to overwrite that method, otherwise I wouldn’t even need it, just use Isnullable anyway

  • @Rod, you know you’re changing the meaning of the show. you’re transforming null in ""-

  • But if that’s what you really want, maybe you can intersect the writing of the attribute xsi:nil="true" superimposing the method WriteAttributeString.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.