Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace Model
- {
- public class Message
- {
- public string Name { get; set; }
- public int Age { get; set; }
- }
- }
- namespace Xamarin
- {
- class Program
- {
- private static string Serialize(List<Model.Message> messages)
- {
- System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(messages.GetType());
- using (StringWriter textWriter = new StringWriter())
- {
- xml.Serialize(textWriter, messages);
- var objs = textWriter.ToString();
- return objs;
- }
- }
- public static List<Model.Message> Deserialize(string messages)
- {
- System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(List<Model.Message>));
- using (StringReader sr = new StringReader(messages))
- {
- var r = xml.Deserialize(sr) as List<Model.Message>;
- return r;
- }
- }
- static void Main(string[] args)
- {
- var messages = new List<Model.Message>()
- {
- new Model.Message{Name = "Hohn", Age = 19},
- new Model.Message{Name = "Anna", Age = 17}
- };
- var str = Serialize(messages);
- var lst = Deserialize(str);
- Console.WriteLine(lst.First().Name);
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement