Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Serialization for Specific Property

I have a class object for XML serialization

[XmlType("PAYMENT")]
public class PaymentXML
{
    [XmlElement(ElementName = "REQUEST")]
    public RequestXML Request { get; set; }

    [XmlElement(ElementName = "META")]
    public MetaXML Request { get; set; }

    //Property that I dont want to be serialized
    public Subscriber Subscriber { get; set; }
}

the serialization

var xml = new PaymentXML();

string path = HttpContext.Current.Server.MapPath(@_xmlResponseDir + _responsePath);

using (var sw = new StreamWriter(path))
{
    var ns = new XmlSerializerNamespaces();
    ns.Add("", "");

    var serializer = new XmlSerializer(typeof(PaymentXML), new XmlRootAttribute("XML"));

    serializer.Serialize(sw, xml, ns);
}

The problem is, it's also serializing the Subscriber property. I only want the RequestXML and MetaXML to be serialized into XML. How can I exclude the Subscriber property in the serialization process?

like image 508
dotnetlinc Avatar asked May 26 '11 15:05

dotnetlinc


2 Answers

Use the [XmlIgnore] attribute:

// Property that I don't want to be serialized.
[XmlIgnore]
public Subscriber Subscriber { get; set; }
like image 122
Frédéric Hamidi Avatar answered Sep 28 '22 07:09

Frédéric Hamidi


Add the XmlIgnore attribute to that property.

like image 30
carlosfigueira Avatar answered Sep 28 '22 07:09

carlosfigueira