序列化为XML时忽略属性

时间:2014-04-16 22:35:33

标签: c# xml xml-serialization xmlserializer

我有一个基类,里面有很多大类。

例如,假设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]类上添加Payment

我只需要在一个方法上使用它,这样我就可以应用覆盖了。但是,仅供您参考,这是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
{

}

1 个答案:

答案 0 :(得分:2)

指定覆盖时,传入包含属性的类型:

using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Xml.Serialization;

namespace WpfApplication10
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var person = new Person
            {
                Payment = new Payment { Amount = 100 },
                Payments = new List<Payment>
                {
                    new Payment { Amount = 200 }, 
                    new Payment { Amount = 400 }
                }
            };

            var attributes = new XmlAttributes { XmlIgnore = true };

            var overrides = new XmlAttributeOverrides();
            overrides.Add(typeof(Person), "Payment", attributes);
            overrides.Add(typeof(Person), "Payments", attributes);

            var serializer = new XmlSerializer(typeof(Person), overrides);
            using (var stringWriter = new StringWriter())
            {
                serializer.Serialize(stringWriter, person);
                string s = stringWriter.ToString();
            }
        }
    }

    public class Person
    {
        public List<Payment> Payments { get; set; }
        public Payment Payment { get; set; }
        public int SomethingElse { get; set; }
    }

    public class Payment
    {
        public decimal Amount { get; set; }
    }
}

结果:

<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SomethingElse>0</SomethingElse>
</Person>