开发者

C# Custom Serialization for Property

I'm Serializing a class with XMLSerializer, But for property "MyDynamicObject" alone i want to override and provide a custom implementation. How can it be done ?

[Serializable]
public class Movie
{
  public string Title
  { get; set; }

  public int Rating
  { get; set; }

  public dynamic MyDynamicObject
  { get; set; }
}


public void SerializeToXML(Movie movie)
{
  XmlSerializer serializer = new XmlSerializer(typeof(Movie));
  TextWriter textWrit开发者_StackOverflow社区er = new StreamWriter(@"C:\movie.xml");
  serializer.Serialize(textWriter, movie);
  textWriter.Close();
}


You could implement the IXMLSerializable which:

Provides custom formatting for XML serialization and deserialization.


You'll want to review the list in Attributes That Control XML Serialization and check for 'properties' in the Applies To column.

We can probably help you more if you're more specific with your requirements.


Take a look at the [OnSerializing()] attribute on MSDN. There is some sample code at the bottom.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜