using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
[XmlRoot(ElementName = "mibscalar")]
[XmlAttribute(AttributeName = "link")]
public string Link { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlElement("bit", typeof(Bit))]
[XmlElement("index", typeof(Index))]
public List<DataItemBase> Items { get; set; }
[XmlAttribute(AttributeName = "index")]
public string Index { get; set; }
[XmlAttribute(AttributeName = "counter")]
public string Counter { get; set; }
[XmlElement("bit", typeof(Bit))]
[XmlElement("index", typeof(Index))]
public List<DataItemBase> Items { get; set; }
public abstract class DataItemBase
public class Index : DataItemBase
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "data")]
public List<Data> Data { get; set; }
public class Bit : DataItemBase
[XmlAttribute(AttributeName = "index")]
public string Index { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
public static void Test()
var root = xml.LoadFromXml<Mibscalar>();
var xml2 = root.GetXml(true);
Console.WriteLine("Deserialized and re-serialized {0}: ", root);
Assert.IsTrue(XNode.DeepEquals(XElement.Parse(xml), XElement.Parse(xml2)), "Original and re-serialized XML are NOT EQUIVALENT!");
Console.WriteLine("Original and re-serialized XML are equivalent.");
var xml = @"<?xml version=""1.0""?>
<mibscalar link=""http://1.1.1.1/v1/mib/objs/inputPointGroupStatus?type=xml"" type=""readonly"" name=""inputPointGroupStatus"">
<data index=""1"" counter=""0"">
<bit index=""1"" value=""false""/>
<bit index=""2"" value=""false""/>
<bit index=""3"" value=""false""/>
<bit index=""4"" value=""false""/>
<bit index=""5"" value=""false""/>
<bit index=""6"" value=""false""/>
<bit index=""7"" value=""false""/>
<bit index=""8"" value=""false""/>
<data index=""2"" counter=""0"" />
<data index=""3"" counter=""0"" />
<data index=""4"" counter=""0"" />
<data index=""5"" counter=""0"" />
<data index=""6"" counter=""0"" />
<data index=""7"" counter=""0"" />
<data index=""8"" counter=""0"" />
<data index=""9"" counter=""0"" />
<data index=""10"" counter=""0"" />
<data index=""11"" counter=""0"" />
<data index=""12"" counter=""0"" />
<data index=""13"" counter=""0"" />
<data index=""14"" counter=""0"" />
<data index=""15"" counter=""0"" />
<data index=""16"" counter=""0"" />
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 class XmlAssert
public static void AreEqual(
Assert.IsTrue(XNode.DeepEquals(Normalize(expected), Normalize(actual)));
private static XElement Normalize(XElement element)
.OrderBy(a => a.Name.ToString()),
.OrderBy(a => a.Name.ToString())
.Select(e => Normalize(e)));
.OrderBy(a => a.Name.ToString()));
return new XElement(element.Name, element.Attributes()
.OrderBy(a => a.Name.ToString()), element.Value);
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
public static void IsTrue(bool value, string message)
throw new AssertionFailedException(message ?? "failed");
public static void Main()
Console.WriteLine("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
Console.WriteLine("Uncaught exception: ");