I have a utility that converts an xml file to a class object:
public static T CreateClassFromXml<T>(string fileName, string root) where T : class
{
fileName.ThrowNullOrEmpty("fileName");
File.Exists(fileName).ThrowFalse(string.Format("File '{0}' could not be found", fileName));
var serializer = new XmlSerializer(typeof(T), new XmlRootAttribute() { ElementName = root });
using (var reader = XmlReader.Create(fileName))
{
return (T)serializer.Deserialize(reader);
}
}
The utility reads the xml and creates a class T. Using the above code is there any way I can validate the created class other than writing a wrapper class around it? I need to ensure that data is populated for all mandatory fields.