1

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
G R
  • 137
  • 1
  • 3
  • 12
  • Possible duplicate with this question: http://stackoverflow.com/questions/9377414/excluding-some-properties-during-serialization-without-changing-the-original-cla. Looks like one solution is to use `XmlAttributeOverrides`. – rsbarro Apr 10 '13 at 05:37
  • but in that there is [xmlignore] used , but as in my case , to serilize only those which are specified xmlelement, as i do not want to modify my orignal class. – G R Apr 10 '13 at 05:57
  • Please see the update to my answer, the second code snippet. Hopefully that should work for you... – rsbarro Apr 10 '13 at 06:39

1 Answers1

1

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; }
}
Joris Onghena
  • 145
  • 1
  • 1
  • 11
rsbarro
  • 27,021
  • 9
  • 71
  • 75