I have following class:
[Serializable]
public class Person
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public int Age { get; set; }
}
Now when I get an XML file from somewhere (eg. web service) I need to deserialize it to the instance of this class. Before doing that I'd like to check the XML file against schema to ensure XML has proper structure. I know I can create schema via XSD.exe tool like this:
xsd.exe MyAssembly.dll /type:Person
This will create a physical .xsd schema file that I can then load to XmlSchema object. However I want to skip this manual generation of schema file and instead create XmlSchema dynamically in memory for a given class. This way when Person class is changed the code that generates XmlSchema directly from it will always have the schema that matches the current definition of the class. This is preferred way for me instead of always manually invoking xsd.exe tool every time Person class is changed.
So is it possible to programmatically create XmlSchema from a Person class?