Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

c# - custom xml serialization

i need help, i have some data coming in xml, i want to make an object out of, do something with it, serialize it back and send it away... but it must be some sort of custom serialization.

xml like:

<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>

needs to be presented as:

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()
  {}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The XmlMagic you are after is called IXmlSerializable interface: http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

It provides you with 2 methods ReadXml and WriteXml, where you have to implement reading your object and writing it back. Then you use the standard .Net XmlSerializer to serialize/deserialize your objects.

Let me know if you need more help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...