public static XElement Serialize(this object source) { //Create our own namespaces for the output XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); //Add an empty namespace and empty value ns.Add("", ""); //try //{ var serializer = XmlSerializerFactory.GetSerializerFor(source.GetType()); var xdoc = new XDocument(); using (var writer = xdoc.CreateWriter()) { serializer.Serialize(writer, source, ns); //, new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") })); } return((xdoc.Document != null) ? xdoc.Document.Root : new XElement("Error", "Document Missing")); //} //catch (Exception x) //{ // return new XElement("Error", x.ToString()); //} }
public static T Deserialize <T>(this XElement source) where T : class { try { var serializer = XmlSerializerFactory.GetSerializerFor(typeof(T)); return((T)serializer.Deserialize(source.CreateReader())); } catch (Exception x) { MessageBox.Show(x.Message + " " + x.InnerException.Message); return(null); } }