我有一个XML序列化的类对象
[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; }
}
序列化
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);
}
问题是,它还序列化Subscriber
属性。我只希望将RequestXML
和MetaXML
序列化为XML。 如何在序列化过程中排除Subscriber
属性?
答案 0 :(得分:30)
使用[XmlIgnore]属性:
// Property that I don't want to be serialized.
[XmlIgnore]
public Subscriber Subscriber { get; set; }
答案 1 :(得分:9)
将XmlIgnore属性添加到该属性。