I am trying to serialize a complete set of Dynamics CRM metdata, using roughly the following code:
RetrieveAllEntitiesRequest metaDataRequest = new RetrieveAllEntitiesRequest()
{
EntityFilters = EntityFilters.All
};
// Execute the request
RetrieveAllEntitiesResponse metaDataResponse = (RetrieveAllEntitiesResponse)service.Execute(metaDataRequest);
entitiesMetadata = metaDataResponse.EntityMetadata;
// Store Metadata for faster usage in the future
Serialize<EntityMetadata[]>(entitiesMetadata, "foobar_metadata.xml");
public static void Serialize<T>(T value, string filePath)
{
if (value == null)
{
return;
}
try
{
var xmlserializer = new XmlSerializer(typeof(T));
var stringWriter = new StringWriter();
using (var writer = new XmlTextWriter(stringWriter) { Formatting = System.Xml.Formatting.Indented })
{
xmlserializer.Serialize(writer, value);
File.WriteAllText(filePath, stringWriter.ToString());
}
}
catch (Exception ex)
{
throw new Exception("An error occurred", ex);
}
}
Yet, some of the metadata classes do not have parameterless constructors. So I get the following exception:[...] cannot be serialized because it does not have a parameterless constructor
I know that the common advice is to extend the affected classes and give them a parameterless constructor. Yet, this could mean many iterations of trial and error as many classes are involved in the Dynamics CRM metadata.
Is there any other way of serializing/deserializing, which doesn't need a parameterless constructor for each class?
Did somebody go through the effort of extending classes for Dynamics CRM metadata and can share the code?
Is there a possibility to automatically omit any non-serializable classes and only serialize the rest?