using System.Collections.Generic;
using System.Xml.Serialization;
public partial class location
[XmlAttribute("nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public bool ShouldSerializeNil() { return nil == true; }
public partial class location
private string cityField;
private string countryField;
private string stateField;
private string textField;
[System.Xml.Serialization.XmlAttributeAttribute()]
[System.Xml.Serialization.XmlAttributeAttribute()]
return this.countryField;
this.countryField = value;
[System.Xml.Serialization.XmlAttributeAttribute()]
[System.Xml.Serialization.XmlTextAttribute()]
[System.Xml.Serialization.XmlRootAttribute("locations", Namespace = "")]
public class LocationList
[XmlElement("location", IsNullable = false)]
public List<location> Locations { get; set; }
[System.Xml.Serialization.XmlRootAttribute("locations", Namespace = "")]
public class LocationListLocationsIsNullableTrue
[XmlElement("location", IsNullable = true)]
public List<location> Locations { get; set; }
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, bool omitStandardNamespaces = false)
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
ns = new XmlSerializerNamespaces();
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings() { Indent = true, IndentChars = " " };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
new XmlSerializer(obj.GetType()).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();
var xml = @"<locations xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi = ""http://www.w3.org/2001/XMLSchema-instance"">
<location country=""PARAGUAY"" city=""Ciudad del Este"" state=""Alto Parana""/>
<location country=""PARAGUAY"" city=""Ciudad del Este"" state=""Alto Parana""/>
<location country=""BRAZIL"" city=""Passo Fundo"" state=""Rio Grande do Sul"" xsi:nil=""true""/>
<location country=""PARAGUAY"" city=""Ciudad del Este"" state=""Alto Parana""/>
<location country=""BRAZIL"" city=""Passo Fundo"" state=""Rio Grande do Sul"" xsi:nil=""true""/>
<location country=""PARAGUAY"" city=""Ciudad del Este"" state=""Alto Parana""/>
<location country=""PARAGUAY"" city=""Ciudad del Este"" state=""Alto Parana""/>
static void Test<T>(string xml)
var root = xml.LoadFromXML<T>();
var xml2 = root.GetXml(false);
Console.WriteLine(string.Format("Deserialized and re-serialized {0} XML: ", typeof(T)));
public static void Main()
Console.WriteLine("Original XML: ");
Test<LocationListLocationsIsNullableTrue>(xml);