自定义xml序列化

AK_*_*AK_ 4 .net c# xml

我需要帮助,我有一些数据来自xml,我想制作一个对象,用它做一些事情,序列化它并发送它...但它必须是某种自定义序列化.

xml喜欢:

<Animals Ver="12" class="1" something="2">
    <Dog Ver="12" class="2" something="17">
        <Name> a </Name>
        <Sound> oof </Sound>
        <SomeOtherProp>12</SomeOtherProp>

    </Dog>
    <Cat Ver="12" class="3" something="4">
       <Name> b </Name>
       <Sound> meow </Sound>
    </Cat>
</Animals>
Run Code Online (Sandbox Code Playgroud)

需要表现为:

abstract class Animal :XmlMagic
{
  public string Name{get;set;}
  public string Sound{get;set;}

  public void SomeMagicalXMLSerializationMethod()
  {}
  public void SomeMagicalXMLDeSerializationMethod()
  {}
}

class Dog: Animal, XmlMagic
{
  public int SomeOtherProp{get;set;}
  public void SomeMagicalXMLSerializationMethod()
  {}
  public void SomeMagicalXMLDeSerializationMethod()
  {}
}
Run Code Online (Sandbox Code Playgroud)

Grz*_*nio 7

XmlMagic你被称为后IXmlSerializable界面:http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

它提供了2种方法ReadXmlWriteXml,在那里你必须实现读你的对象,并写回.然后使用标准.Net XmlSerializer来序列化/反序列化对象.

如果您需要更多帮助,请告诉我.