gpt4 book ai didi

c# - 禁用特定属性的序列化

转载 作者:可可西里 更新时间:2023-11-01 08:14:46 26 4
gpt4 key购买 nike

我有一个用于 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 属性。我只希望将 RequestXMLMetaXML 序列化为 XML。 如何在序列化过程中排除 Subscriber 属性?

最佳答案

使用 [XmlIgnore]属性:

// Property that I don't want to be serialized.
[XmlIgnore]
public Subscriber Subscriber { get; set; }

关于c# - 禁用特定属性的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6141205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com