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;
namespace Question42114531.V1
[XmlRoot(ElementName = "Address")]
[XmlElement(ElementName = "Street1")]
public string Street1 { get; set; }
[XmlElement(ElementName = "City")]
public string City { get; set; }
[XmlElement(ElementName = "State")]
public string State { get; set; }
[XmlElement(ElementName = "Postal")]
public string Postal { get; set; }
[XmlRoot(ElementName = "Contact")]
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Phone")]
public string Phone { get; set; }
[XmlElement(ElementName = "Address")]
public Address Address { get; set; }
[XmlRoot(ElementName = "Contacts")]
[XmlElement(ElementName = "Contact")]
public Contact Contact { get; set; }
[XmlRoot("GenericRequestData_Type")]
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public List<string> Emails { get; set; }
public List<long> IssueIds { get; set; }
public class GenericRequestData_Type
private System.Xml.XmlElement[] anyField;
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlElement[] Any
internal static void Test()
var genericRequest = new GenericRequestData_Type();
var contacts = new Contacts
genericRequest.Any = new[] { contacts.AsXmlElement() };
Console.WriteLine(genericRequest.GetXml());
public static class XmlNodeExtensions
public static XmlDocument AsXmlDocument<T>(this T o, XmlSerializerNamespaces ns = null, XmlSerializer serializer = null)
XmlDocument doc = new XmlDocument();
using (XmlWriter writer = doc.CreateNavigator().AppendChild())
new XmlSerializer(o.GetType()).Serialize(writer, o, ns ?? NoStandardXmlNamespaces());
public static XmlElement AsXmlElement<T>(this T o, XmlSerializerNamespaces ns = null, XmlSerializer serializer = null)
return o.AsXmlDocument(ns, serializer).DocumentElement;
public static T Deserialize<T>(this XmlElement element, XmlSerializer serializer = null)
using (var reader = new ProperXmlNodeReader(element))
return (T)(serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader);
public static XmlSerializerNamespaces NoStandardXmlNamespaces()
var ns = new XmlSerializerNamespaces();
public class ProperXmlNodeReader : XmlNodeReader
public ProperXmlNodeReader(XmlNode node) : base(node) { }
public override string LookupNamespace(string prefix)
return NameTable.Add(base.LookupNamespace(prefix));
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 static void Main()
Question42114531.V1.TestClass.Test();