【发布时间】:2014-04-16 22:35:33
【问题描述】:
我有一个基类,里面有很多大类。
例如,假设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 Class XML 属性。例如,我不允许在Payment 类上添加[XmlIgnore]。
我只需要在一种方法上使用它,因此我想在其中应用覆盖。但是,仅供参考,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
{
}
【问题讨论】:
-
是的,我已经看到了。我想忽略整个班级,而不仅仅是一个财产。请看看我试过什么。它不工作。
-
你的类最终被表示为一个属性。
-
那我该怎么办? xOver.Add(typeof(Person), "支付", att); ?是这样的吗?另外,如果是这样呢? List
RequiredPayments { get;放; } ? -
我添加了一个带有列表的示例。
标签: c# xml xml-serialization xmlserializer