我有一个基类,里面有很多大类。
例如,假设Person
班级。在它的内部,有一个Payment
类,里面有一个CreditCard
上课,等等……
我正在尝试序列化Person
类,我想排除其中的某些类。
在这个例子中,我试图序列化Person
类并忽略整个付款类。这就是我到目前为止所做的,但它不起作用。
你们能帮我弄清楚如何实现这一目标吗?谢谢
XmlAttributes att = new XmlAttributes { XmlIgnore = true };
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
xOver.Add(typeof(Payment), "Payment", att);
SerializeContractsRequest(items, xOver);
public static string Serialize<T>(T clrObject, XmlAttributeOverrides xmlOverrides) where T : class, new()
{
XmlSerializer xs = xmlOverrides == null ? new XmlSerializer(typeof(T)) : new XmlSerializer(typeof(T), xmlOverrides);
string xml = string.Empty;
//A string builder that will hold the converted business object as an xml string
StringBuilder sb = new StringBuilder();
//The stream that will write the serialized xml to the stringbuilder object
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
XmlWriter writer = XmlWriter.Create(sb, settings);
xs.Serialize(writer, clrObject);
xml = sb.ToString();
return xml;
}
另外,我不被允许触摸Payment
类 XML 属性。例如,我不允许添加[XmlIgnore]
on Payment
Class.
我只需要在一种方法上使用此方法,因此这就是我想要应用覆盖的地方。不过,仅供参考,这就是Payment
类有:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="Payment", Namespace="http://schemas.datacontract.org/2004/07/ServicesLayer")]
[System.SerializableAttribute()]
public partial class Payment
{
}