using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
[XmlRoot(ElementName = "Weight")]
[XmlElement(ElementName = "Weight_A", IsNullable = true)]
public string Weight_A { get; set; }
public bool ShouldSerializeWeight_A() { return Weight_A != null; }
[XmlElement(ElementName = "Weight_B", IsNullable = true)]
public string Weight_B { get; set; }
public bool ShouldSerializeWeight_B() { return Weight_B != null; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
[XmlRoot(ElementName = "Density")]
[XmlElement(ElementName = "Density_A", IsNullable = true)]
public string Density_A { get; set; }
public bool ShouldSerializeDensity_A() { return Density_A != null; }
[XmlElement(ElementName = "Density_B", IsNullable = true)]
public string Density_B { get; set; }
public bool ShouldSerializeDensity_B() { return Density_B != null; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
[XmlRoot(ElementName = "Volume")]
[XmlElement(ElementName = "Volume_A")]
public string Volume_A { get; set; }
[XmlElement(ElementName = "Volume_B")]
public string Volume_B { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
[XmlRoot(ElementName = "Material")]
[XmlElement(ElementName = "MaterialName")]
public string MaterialName { get; set; }
[XmlElement(ElementName = "Weight")]
public Weight Weight { get; set; }
[XmlElement(ElementName = "Density")]
public Density Density { get; set; }
[XmlElement(ElementName = "Volume")]
public Volume Volume { get; set; }
[XmlRoot(ElementName = "Materials")]
[XmlElement(ElementName = "Material")]
public List<Material> Material { get; set; }
public static void Test()
var root = xml.LoadFromXml<Materials>();
var xml2 = root.GetXml();
Console.WriteLine("\nDeserialized and re-serialized {0}:", root);
Assert.IsTrue(!xml2.Contains(@"nil=""true"""));
var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Materials xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<MaterialName>ABC</MaterialName>
<Weight_A xsi:nil=""true"" />
<Weight_B xsi:nil=""true"" />
<Density_A xsi:nil=""true"" />
<Density_B xsi:nil=""true"" />
<Volume Value=""8771.427"" />
<MaterialName>ABC</MaterialName>
<V5_Weight>2.009</V5_Weight>
<V6_Weight>1.3318154561904</V6_Weight>
<V5_density>1000</V5_density>
<V6_density>663</V6_density>
<Volume Value=""2008771.427"" />
public static class XmlSerializationHelper
public static T LoadFromXml<T>(this string xmlString, XmlSerializer serial = null)
serial = serial ?? new XmlSerializer(typeof(T));
using (var reader = new StringReader(xmlString))
return (T)serial.Deserialize(reader);
public static string GetXml<T>(this T obj, bool omitStandardNamespaces)
return obj.GetXml(null, omitStandardNamespaces);
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 static string GetOuterXml(this XmlNode node, bool indent = true)
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings
OmitXmlDeclaration = true,
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
return textWriter.ToString();
public static void Main()
Console.WriteLine("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
Console.WriteLine("Uncaught exception: ");