在学习后我无法在课程中序列化私有属性时,我开始使用 DataContract 和 DataMember 注释。
我的类中有一个私有字符串属性,我想将其序列化为属性而不是元素。
[DataContract]
public class Channel
{
private string m_Name = string.Empty;
private DateTime? m_TxTime = null;
[DataMember, XmlAttribute("name")]
public string Name
{
get
{
return m_Name;
}
set
{
m_Name = value;
}
}
public DateTime? TxTime
{
get
{
return m_TxTime;
}
set
{
m_TxTime = value;
}
}
[DataMember, XmlAttribute("txTime")]
private string txTimeForSerialization
{
get
{
if (TxTime.HasValue)
{
return TxTime.Value.ToString("o");
}
else
{
return null;
}
}
}
}
尝试使用 DataMember 和 XmlAttribute 注释一起给了我这个输出:
<channel name="NAME">
<TxTime>2017-03-26T13:15:56.8042989+03:00</TxTime>
</channel>
我想要的结果就是这个 -
<channel name="NAME" txTime="2017-03-26T13:15:56.8042989+03:00" />
我序列化的方式是使用XmlSerializer -
using (FileStream fs = new FileStream("channels.txt", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(Channel));
serializer.Serialize(fs, objectName);
}
答案 0 :(得分:0)
为您的类创建“包装器”,它将负责正确的反序列化格式(某种Facade模式)
[DataContract]
public class ChannelData
{
[DataMember, XmlAttribute("name")]
public string Name { get; set; }
[DataMember, XmlAttribute("txTime")]
public string txTimeForSerialization { get; set; }
}
您可以在Channel
类中创建创建序列化实例的逻辑,然后责任将序列化逻辑保留在Channel类中 - 这将保持封装。
public class Channel
{
public string Name { get; set; }
public DateTime? TxTime { get; set; }
public Channel()
{
Name = string.Empty;
TxtTime = null;
}
public ChannelData ToSerialization()
{
var data = new ChannelData();
data.Name = Name;
data.txTimeForSerialization = TxTime.HasValue ? TxTime.Value.ToString("o") : null;
}
}
使用包装器,您不需要考虑不同格式的不同“解决方法”,您的“业务层”类将保持“清除”,包装器将处理格式化。
答案 1 :(得分:0)
如果您想坚持使用当前的Channel
类,则需要实现IXmlSerializer
接口及其 WriteXml 方法。这是您通过XmlSerializer
序列化私有媒体资源的唯一方法。
public class Channel : IXmlSerializable
{
private string m_Name = string.Empty;
private DateTime? m_TxTime = null;
public string Name
{
get
{
return m_Name;
}
set
{
m_Name = value;
}
}
public DateTime? TxTime
{
get
{
return m_TxTime;
}
set
{
m_TxTime = value;
}
}
private string txtTimeForSerialization
{
get
{
if (TxTime.HasValue)
{
return TxTime.Value.ToString("o");
}
else
{
return null;
}
}
}
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
//implement reader if needed...
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("name", this.Name);
writer.WriteAttributeString("txtTime", this.txtTimeForSerialization);
}
}
通过实施IXmlSerializable
,您不再需要使用属性,而且用于写入 channels.txt 的代码将保持不变:
using (FileStream fs = new FileStream("channels.txt", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(Channel));
serializer.Serialize(fs, objectName);
}
这是输出:
<?xml version="1.0"?>
<Channel name="NAME" txtTime="2017-03-26T12:57:25.6780078Z" />