G R
G R

Reputation: 137

Serialize only those properties which are specified [XmlElement] without changing the original class

Code:

[Serializable]
public class MyClass
{
    [XmlElement("Company")]
    public string Company { get; set; }

    [XmlElement("Amount")]
    public decimal Amount { get; set; }

    public int companyid { get; set; }
}

Now I want to serilize only thoese which are specified with [XmlElement], companyid not to be serilized.

So, what can I do?

Upvotes: 1

Views: 2794

Answers (1)

rsbarro
rsbarro

Reputation: 27339

Here's a simple example I put together in LinqPad. The first 4 lines of the Main method set up an XmlAttributeOverrides instance that is then used to tell the XmlSerializer to not serialize the companyid property.

void Main()
{
    //Serialize, but ignore companyid
    var overrides = new XmlAttributeOverrides();
    var attributes = new XmlAttributes();
    attributes.XmlIgnore = true;
    overrides.Add(typeof(MyClass), "companyid", attributes);

    using(var sw = new StringWriter()) {
        var xs = new XmlSerializer(typeof(MyClass), overrides);
        var a = new MyClass() { 
                                       Company = "Company Name", 
                                       Amount = 10M, 
                                       companyid = 7 
                                   };
        xs.Serialize(sw, a);
        Console.WriteLine(sw.ToString());
    }
}

[Serializable]
public class MyClass
{
    [XmlElement("Company")]
    public string Company { get; set; }

    [XmlElement("Amount")]
    public decimal Amount { get; set; }

    public int companyid { get; set; }
}

The output of this program is:

<?xml version="1.0" encoding="utf-16"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Company>Company Name</Company>
  <Amount>10</Amount>
</MyClass>

If you need this code to inspect the class to determine which properties to exclude based on an XmlElementAttribute not being present, then you can modify the above code to use reflection to enumerate the properties of the object. For every property that does not have an XmlElementAttribute, add an item to the overrides instance.

For example:

void Main()
{
    //Serialize, but ignore properties that do not have XmlElementAttribute
    var overrides = new XmlAttributeOverrides();
    var attributes = new XmlAttributes();
    attributes.XmlIgnore = true;
    foreach(var prop in typeof(MyClass).GetProperties())
    {
        var attrs = prop.GetCustomAttributes(typeof(XmlElementAttribute));
        if(attrs.Count() == 0)
            overrides.Add(prop.DeclaringType, prop.Name, attributes);
    }

    using(var sw = new StringWriter()) {
        var xs = new XmlSerializer(typeof(MyClass), overrides);
        var a = new MyClass() { 
                                Company = "Company Name", 
                                Amount = 10M, 
                                companyid = 7,
                                blah = "123" };
        xs.Serialize(sw, a);
        Console.WriteLine(sw.ToString());
    }
}

[Serializable]
public class MyClass
{
    [XmlElement("Company")]
    public string Company { get; set; }

    [XmlElement("Amount")]
    public decimal Amount { get; set; }

    public int companyid { get; set; }

    public string blah { get; set; }
}

Upvotes: 1

Related Questions