控制XML序列化格式的具体方法 [英] Control XML Serialization format with a specific method

查看:206
本文介绍了控制XML序列化格式的具体方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DateTime和我想的XML序列化格式的具体子类。通常情况下,不指定任何东西,一个DateTime的系列化将只需按照目前的文化,但我想要的DateTime格式化以某种方式(即使不是deserializable),同上子类。



因此,考虑到这些类:

 公共类MyClass的
{
公众的DateTime MyDateTime {搞定;组; }
公共MySubClass TheSubClass {搞定;组; }
}

公共类MySubClass
{
公众诠释ID {搞定;组; }
公共字符串名称{;组; }
}



我如何指定序列化方法是将输出:

 < MyClass的> 
< MyDateTime> 2011-9-13T10:30:00Z< / MyDateTime>
< MySubClass> ID-名称< / MySubClass>
< / MyClass的>


解决方案

您使用XmlSerializer的?如果是这样,你并不需要包括[Serializable接口]属性,它们是由XmlSerializer的忽略。您可以通过实现你的类型IXmlSerializable接口自定义序列化。



http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx


I have a DateTime and a subclass that I want formatted specifically on XML serialization. Normally, without specifying anything, serialization of a DateTime would just follow the current culture, but I want DateTime formatted in a certain way (even if not deserializable), ditto the subclass.

So, given these classes:

public class MyClass
{
    public DateTime MyDateTime { get; set; }
    public MySubClass TheSubClass { get; set; }
}

public class MySubClass 
{
    public int ID { get; set; }
    public string Name { get; set; }
}

How do I specify serialization methods that would output:

<MyClass>
  <MyDateTime>2011-9-13T10:30:00Z</MyDateTime>
  <MySubClass>ID-Name</MySubClass>
</MyClass>

解决方案

Are you utilizing XmlSerializer? If so, you do not need to include the [Serializable] attributes, they are ignored by XmlSerializer. You can customize the serialization by implementing the IXmlSerializable interface on your type.

http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

这篇关于控制XML序列化格式的具体方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆