using System.Collections.Generic;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Collections;
[XmlRoot("ArrayOfSounds")]
public CSoundsRoot() { this.Sounds = new List<CSound>(); }
public List<CSound> Sounds { get; set; }
public string id { get; set; }
public string name { get; set; }
public string file { get; set; }
public string fav { get; set; }
var xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<ArrayOfSounds xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<file>SampleFileName1</file>
<file>SampleFileName2</file>
<file>SampleFileName3</file>
<file>SampleFileName4</file>
<file>SampleFileName5</file>
internal static void Test()
var sounds = new List<CSound> { new CSound { id = "one" } };
var xml1 = sounds.GetXml(null, true);
Console.WriteLine("\nResults of serializing an object of type {0}", sounds.GetType());
var root = new CSoundsRoot { Sounds = sounds };
var xml2 = root.GetXml(null, true);
Console.WriteLine("\nResults of serializing an object of type {0}", root.GetType());
var fullRoot = fullXml.LoadFromXml<CSoundsRoot>();
var fullXml2 = fullRoot.GetXml(null, true);
Console.WriteLine("\nResult of deserializing and re-serializing the XML from question 42040325 into an object of type {0}", fullRoot.GetType());
Console.WriteLine(fullXml2);
public static void Main()
public static class XmlSerializationHelper
public static T LoadFromXml<T>(this string xmlString)
using (StringReader reader = new StringReader(xmlString))
return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
public static string GetXml<T>(this T obj, XmlSerializer serializer = null, bool omitStandardNamespaces = false)
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
ns = new XmlSerializerNamespaces();
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings() { Indent = true };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
(serializer ?? new XmlSerializer(obj.GetType())).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
throw new AssertionFailedException("failed");