You have to read the entity metadata from your XML one by one and create the corresponding entities/fields.
Here are a few snippets to create attributes(fields) from one of my projects that could save you some time:
public void CreateAttribute(AttributeMetadata attribute, string targetEntity)
{
dynamic newattribute = null;
switch (attribute.AttributeType.Value)
{
case AttributeTypeCode.Boolean:
newattribute = CreateBoolAttribute(attribute);
break;
case AttributeTypeCode.DateTime:
newattribute = CreateDateTimeAttribute(attribute);
break;
case AttributeTypeCode.Decimal:
newattribute = CreateDecimalAttribute(attribute);
break;
case AttributeTypeCode.Integer:
newattribute = CreateIntegerAttribute(attribute);
break;
case AttributeTypeCode.String:
newattribute = CreateStringAttribute(attribute);
break;
case AttributeTypeCode.Picklist:
newattribute = CreatePickListAttribute(attribute);
break;
case AttributeTypeCode.Memo:
newattribute = CreateMemoAttribute(attribute);
break;
case AttributeTypeCode.Money:
newattribute = CreateMoneyAttribute(attribute);
break;
default:
break;
}
if (newattribute != null)
{
CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
{
EntityName = string.IsNullOrEmpty(targetEntity) ? attribute.EntityLogicalName : targetEntity,
Attribute = newattribute
};
Service.Execute(createAttributeRequest);
}
}
private MoneyAttributeMetadata CreateMoneyAttribute(AttributeMetadata oldattribute)
{
var attribute = (MoneyAttributeMetadata)oldattribute;
MoneyAttributeMetadata moneyAttribute = new MoneyAttributeMetadata
{
LogicalName = attribute.LogicalName,
SchemaName = attribute.LogicalName,
DisplayName = attribute.DisplayName,
RequiredLevel = attribute.RequiredLevel,
Description = attribute.Description,
ImeMode = attribute.ImeMode,
MaxValue = attribute.MaxValue,
MinValue = attribute.MinValue,
Precision = attribute.Precision,
PrecisionSource = attribute.PrecisionSource
};
return moneyAttribute;
}
private MemoAttributeMetadata CreateMemoAttribute(AttributeMetadata oldattribute)
{
var attribute = (MemoAttributeMetadata)oldattribute;
MemoAttributeMetadata memoAttribute = new MemoAttributeMetadata
{
LogicalName = attribute.LogicalName,
SchemaName = attribute.LogicalName,
DisplayName = attribute.DisplayName,
RequiredLevel = attribute.RequiredLevel,
Description = attribute.Description,
Format = StringFormat.TextArea,
ImeMode = attribute.ImeMode,
MaxLength = attribute.MaxLength
};
return memoAttribute;
}
private PicklistAttributeMetadata CreatePickListAttribute(AttributeMetadata oldattribute)
{
var attribute = (PicklistAttributeMetadata)oldattribute;
PicklistAttributeMetadata picklistAttribute = new PicklistAttributeMetadata
{
LogicalName = attribute.LogicalName,
SchemaName = attribute.LogicalName,
DisplayName = attribute.DisplayName,
RequiredLevel = attribute.RequiredLevel,
Description = attribute.Description,
OptionSet = attribute.OptionSet,
DefaultFormValue = attribute.DefaultFormValue
};
return picklistAttribute;
}
private StringAttributeMetadata CreateStringAttribute(AttributeMetadata oldattribute)
{
var attribute = (StringAttributeMetadata)oldattribute;
StringAttributeMetadata stringAttribute = new StringAttributeMetadata
{
LogicalName = attribute.LogicalName,
SchemaName = attribute.LogicalName,
DisplayName = attribute.DisplayName,
RequiredLevel = attribute.RequiredLevel,
Description = attribute.Description,
Format = attribute.Format,
ImeMode = attribute.ImeMode,
MaxLength = attribute.MaxLength
};
return stringAttribute;
}
private IntegerAttributeMetadata CreateIntegerAttribute(AttributeMetadata oldattribute)
{
var attribute = (IntegerAttributeMetadata)oldattribute;
IntegerAttributeMetadata integerAttribute = new IntegerAttributeMetadata
{
LogicalName = attribute.LogicalName,
SchemaName = attribute.LogicalName,
DisplayName = attribute.DisplayName,
RequiredLevel = attribute.RequiredLevel,
Description = attribute.Description,
Format = attribute.Format,
MaxValue = attribute.MaxValue,
MinValue = attribute.MinValue
};
return integerAttribute;
}
private DecimalAttributeMetadata CreateDecimalAttribute(AttributeMetadata oldattribute)
{
var attribute = (DecimalAttributeMetadata)oldattribute;
DecimalAttributeMetadata decimalAttribute = new DecimalAttributeMetadata
{
LogicalName = attribute.LogicalName,
SchemaName = attribute.LogicalName,
DisplayName = attribute.DisplayName,
RequiredLevel = attribute.RequiredLevel,
Description = attribute.Description,
MaxValue = attribute.MaxValue,
MinValue = attribute.MinValue,
Precision = attribute.Precision
};
return decimalAttribute;
}
private DateTimeAttributeMetadata CreateDateTimeAttribute(AttributeMetadata oldattribute)
{
var attribute = (DateTimeAttributeMetadata)oldattribute;
DateTimeAttributeMetadata dtAttribute = new DateTimeAttributeMetadata
{
LogicalName = attribute.LogicalName,
SchemaName = attribute.LogicalName,
DisplayName = attribute.DisplayName,
RequiredLevel = attribute.RequiredLevel,
Description = attribute.Description,
Format = attribute.Format,
ImeMode = attribute.ImeMode
};
return dtAttribute;
}
private BooleanAttributeMetadata CreateBoolAttribute(AttributeMetadata oldattribute)
{
var attribute = (BooleanAttributeMetadata)oldattribute;
BooleanAttributeMetadata boolAttribute = new BooleanAttributeMetadata
{
LogicalName = attribute.LogicalName,
SchemaName = attribute.LogicalName,
DisplayName = attribute.DisplayName,
RequiredLevel = attribute.RequiredLevel,
Description = attribute.Description,
OptionSet = attribute.OptionSet
};
return boolAttribute;
}
private LookupAttributeMetadata CreateLookupAttribute(AttributeMetadata oldattribute)
{
var attribute = (LookupAttributeMetadata)oldattribute;
LookupAttributeMetadata lookupAttribute = new LookupAttributeMetadata
{
LogicalName = attribute.LogicalName,
SchemaName = attribute.LogicalName,
DisplayName = attribute.DisplayName,
RequiredLevel = attribute.RequiredLevel,
Description = attribute.Description,
Targets = attribute.Targets,
IsValidForAdvancedFind = attribute.IsValidForAdvancedFind,
SourceType = attribute.SourceType
};
return lookupAttribute;
}