开发者

C# Custom Serialization for Property

开发者 https://www.devze.com 2023-03-25 22:36 出处:网络
I\'m Serializing a class with XMLSerializer, But for property \"MyDynamicObject\" alone i want to overrideand provide a custom implementation.

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

精彩评论

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