Created
February 5, 2014 20:18
-
-
Save pwdonald/8832195 to your computer and use it in GitHub Desktop.
C# Xml Deserialize to Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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