Skip to content

Instantly share code, notes, and snippets.

@pwdonald
Created February 5, 2014 20:18
Show Gist options
  • Save pwdonald/8832195 to your computer and use it in GitHub Desktop.
Save pwdonald/8832195 to your computer and use it in GitHub Desktop.
C# Xml Deserialize to Model
// XML DESERIALIZER
// Used for deserializing XML serialized by the default C# serializer into Models
// that match the XML structure.
public static T XmlDeserializeFromString<T>(string objectData)
{
return (T)XmlDeserializeFromString(objectData, typeof(T), typeof(T).Name);
}
public static object XmlDeserializeFromString(string objectData, Type type, string rootname)
{
XmlRootAttribute xmlroot = new XmlRootAttribute();
xmlroot.IsNullable = true;
// uncomment if you need to specify the namespace
//if (type.Name == "Error")
//{
// xmlroot.Namespace = "http://www.regions.com/DNLDS/Common";
//}
var serializer = new XmlSerializer(type,xmlroot);
object result;
using (TextReader reader = new StringReader(objectData))
{
result = serializer.Deserialize(reader);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment