For example I want to remove or change below property attributes or add a new one. Is it possible?
[XmlElement("bill_info")]
[XmlIgnore]
public BillInfo BillInfo
{
  get { return billInfo; }
  set { billInfo = value; }
}
                You cannot add a new property() to an instance at runtime, because properties are data descriptors. Instead you must dynamically create a new class, or overload __getattribute__ in order to process data descriptors on instances.
In C#, attributes are classes that inherit from the Attribute base class. Any class that inherits from Attribute can be used as a sort of "tag" on other pieces of code. For instance, there is an attribute called ObsoleteAttribute . This is used to signal that code is obsolete and shouldn't be used anymore.
Attributes are used to impose conditions or to increase the efficiency of a piece of code. There are built-in attributes present in C# but programmers may create their own attributes, such attributes are called Custom attributes. To create custom attributes we must construct classes that derive from the System.
(edit - I misread the original question)
You cannot add actual attributes (they are burned into the IL); however, with XmlSerializer you don't have to - you can supply additional attributes in the constructor to the XmlSerializer. You do, however, need to be a little careful to cache the XmlSerializer instance if you do this, as otherwise it will create an additional assembly per instance, which is a bit leaky. (it doesn't do this if you use the simple constructor that just takes a Type). Look at XmlAttributeOverrides.
For an example:
using System;
using System.Xml.Serialization;
 public class Person
{
    static void Main()
    {
        XmlAttributeOverrides overrides = new XmlAttributeOverrides();
        XmlAttributes attribs = new XmlAttributes();
        attribs.XmlIgnore = false;
        attribs.XmlElements.Add(new XmlElementAttribute("personName"));
        overrides.Add(typeof(Person), "Name", attribs);
        XmlSerializer ser = new XmlSerializer(typeof(Person), overrides);
        Person person = new Person();
        person.Name = "Marc";
        ser.Serialize(Console.Out, person);
    }
    private string name;
    [XmlElement("name")]
    [XmlIgnore]
    public string Name { get { return name; } set { name = value; } }
}
Note also; if the xml attributes were just illustrative, then there is a second way to add attributes for things related to data-binding, by using TypeDescriptor.CreateProperty and either ICustomTypeDescriptor or TypeDescriptionProvider. Much more complex than the xml case, I'm afraid - and doesn't work for all code - just code that uses the component-model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With