Skip to content

Instantly share code, notes, and snippets.

@pinscript
Created April 7, 2011 12:31
Show Gist options
  • Save pinscript/907665 to your computer and use it in GitHub Desktop.
Save pinscript/907665 to your computer and use it in GitHub Desktop.
using System;
using System.Xml.Serialization;
using System.IO;
namespace Scrap
{
class Program
{
static void Main(string[] args)
{
string s = "<config><update_mins>23</update_mins></config>";
Console.WriteLine("Deserializing static xml");
XmlSerializer searializer = new XmlSerializer(typeof(Config));
Config result = (Config)searializer.Deserialize(new StringReader(s));
Console.WriteLine("Minutes: " + result.Minutes);
Console.WriteLine("Seconds: " + result.Seconds);
Console.WriteLine();
Console.WriteLine();
StringWriter s2w = new StringWriter();
searializer.Serialize(s2w, result);
Console.WriteLine(s2w.ToString());
Console.WriteLine("Deserializing generated xml (only seconds)");
XmlSerializer de = new XmlSerializer(typeof (Config));
Config deserialized = (Config) de.Deserialize(new StringReader(s2w.ToString()));
Console.WriteLine("Minutes: " + deserialized.Minutes);
Console.WriteLine("Seconds: " + deserialized.Seconds);
Console.ReadKey(true);
}
}
[XmlRoot(ElementName = "config")]
public class Config
{
[XmlElement(ElementName = "update_mins")]
public int Minutes
{
get
{
return this.Seconds / 60;
}
set
{
this.Seconds = value * 60;
}
}
[XmlElement(ElementName = "update_secs")]
public int Seconds { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment