using System.Collections.Generic;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.Collections;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "C")]
public partial class OCITable
private string[] colHeadingField;
[System.Xml.Serialization.XmlElementAttribute("colHeading", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string[] colHeading
return this.colHeadingField;
this.colHeadingField = value;
[System.Xml.Serialization.XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public OCITableRow[] row { get; set; }
[System.Xml.Serialization.XmlElementAttribute("col", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
public string[] col { get; set; }
[XmlRoot(Namespace = "C")]
public class BroadsoftDocument
[System.Xml.Serialization.XmlElementAttribute("command", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public Command [] Command { get; set; }
[XmlElement("userTable", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public OCITable UserTable { get; set; }
var xml = @"<?xml version=""1.0"" encoding=""ISO-8859-1"" ?>
<BroadsoftDocument protocol=""OCI"" xmlns=""C"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<sessionId xmlns="""">feajiofefeaij</sessionId>
<command echo="""" xmlns="""">
<colHeading>User Id</colHeading>
<colHeading>Group Id</colHeading>
<colHeading>Name</colHeading>
internal static void Test()
Console.WriteLine("Original XML: ");
Console.WriteLine(oldXml);
var doc = oldXml.LoadFromXML<BroadsoftDocument>();
var newXml = doc.GetXml();
Console.WriteLine("Reserialized XML: ");
Console.WriteLine(newXml);
var oldTableXml = XElement.Parse(oldXml).Elements("command").Elements("userTable").FirstOrDefault();
var newTableXml = XElement.Parse(newXml).Elements("command").Elements("userTable").FirstOrDefault();
if (XNode.DeepEquals(oldTableXml, newTableXml))
Console.WriteLine("Original and reserialized {0} elements are equivalent.", oldTableXml.Name);
throw new InvalidOperationException("XNode.DeepEquals(oldTableXml, newTableXml)");
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, 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))
new XmlSerializer(obj.GetType()).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();