Пример #1
0
 private static void SavePerson()
 {
     var person = new Person() { Name = "Bob", Age = 35, Children = new List<string> { "Chris", "Alice" } };
     var serializer = new XmlSerializer(typeof(Person));
     using (var fs = new FileStream(GetFileName(), FileMode.Create))
     {
         serializer.Serialize(fs, person);
     }
 }
Пример #2
0
        private static void SerializationTest()
        {
            Person p1 = new Person() { Name = "Person 1", Age = 20 };
            Person p2 = new Person() { Name = "Person 2", Age = 60 };

            XmlSerializer serializer = new XmlSerializer(typeof(Person));
            serializer.Serialize(Console.OpenStandardOutput(), p1);

            Console.WriteLine();
            Console.WriteLine();

            serializer.Serialize(Console.OpenStandardOutput(), p2);
        }
Пример #3
0
        private void ServiceStackSerialization()
        {
            var person = new Person() { Name = "Bob", Age = 35, Children = new List<string> { "Chris", "Alice" } };

            var xml = new XDocument();
            var stream = new MemoryStream();
            using (var reader = new StreamReader(stream))
            {
                ServiceStack.Text.XmlSerializer.SerializeToStream(person, stream);

                xml = XDocument.Load(reader);
            }

            var deserialisedPerson = ServiceStack.Text.XmlSerializer.DeserializeFromString<Person>(xml.ToString());
            Console.WriteLine(person);
            Console.WriteLine(deserialisedPerson);
        }