问题
I have a base class that has MANY big classes inside it.
For example, Let's say Person
class. Inside it, there is a Payment
Class, inside there is a CreditCard
class, and so on...
I am trying to serialize Person
class, and I would like to exclude certain classes inside it.
In this example, I am trying to serialize Person
class and ignore the ENTIRE payment class. This is what I did so far, but it's not working.
Can you guys help me figure out how I can achieve this? Thanks
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;
}
Also, I am not allowed to touch the Payment
Class XML Attribute. For example, I am not allowed to add [XmlIgnore]
on Payment
Class.
I only need this on one method so that's where I would like to apply the override. However, just for your reference, this is what Payment
class has:
[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:
When you specify an override, you pass-in the type that contains the property :
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; }
}
}
Result :
<?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>
来源:https://stackoverflow.com/questions/23121400/ignore-a-property-when-serializing-to-xml