C# 序列化为XML时忽略属性
我有一个基类,里面有很多大类 例如,让我们假设C# 序列化为XML时忽略属性,c#,xml,xml-serialization,xmlserializer,C#,Xml,Xml Serialization,Xmlserializer,我有一个基类,里面有很多大类 例如,让我们假设Personclass。里面有一个支付类类,里面有一个信用卡类类,等等 我正在尝试序列化Person类,我想排除其中的某些类 在本例中,我试图序列化Person类,并忽略整个payment类。到目前为止,我就是这么做的,但它不起作用 你们能帮我想一想怎样才能做到这一点吗?谢谢 XmlAttributes att = new XmlAttributes { XmlIgnore = true }; XmlAttribute
Person
class。里面有一个支付类
类,里面有一个信用卡类
类,等等
我正在尝试序列化Person
类,我想排除其中的某些类
在本例中,我试图序列化Person
类,并忽略整个payment类。到目前为止,我就是这么做的,但它不起作用
你们能帮我想一想怎样才能做到这一点吗?谢谢
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;
}
指定替代时,将传入包含属性的类型:
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; }
}
}
使用System.Collections.Generic;
使用System.IO;
使用System.Windows;
使用System.Xml.Serialization;
命名空间WpfApplication10
{
公共部分类主窗口
{
公共主窗口()
{
初始化组件();
加载+=主窗口\u加载;
}
已加载私有void主窗口(对象发送器、路由目标)
{
var person=新的人
{
付款=新付款{金额=100},
付款=新列表
{
新付款{金额=200},
新付款{金额=400}
}
};
var attributes=newxmldattributes{XmlIgnore=true};
var overrides=new XmlAttributeOverrides();
覆盖。添加(类型(人员),“付款”,属性);
覆盖。添加(类型(人员),“付款”,属性);
var serializer=新的XmlSerializer(typeof(Person),重写);
使用(var stringWriter=new stringWriter())
{
序列化程序。序列化(stringWriter,person);
字符串s=stringWriter.ToString();
}
}
}
公共阶层人士
{
公共列表付款{get;set;}
公共支付{get;set;}
公共int SomethingElse{get;set;}
}
公共类支付
{
公共十进制数{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>
0
是的,我已经看到了。我想忽略整个类,而不仅仅是一个属性。请看看我试过什么。它不起作用。你的类最终被表示为一个属性。那么我该怎么办?加上(人的类型),“付款”,附件)?是这样的吗?还有,如果是这样呢?List RequiredPayments{get;set;}?我添加了一个带有列表的示例。我完全是这样做的,它对我仍然不起作用,但我确信代码工作得很完美。问题是这个项目一团糟,所以我会继续调试。非常感谢。祝您在调试项目时好运:D
<?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>