Your IP : 216.73.216.48
public static string SerializeObject<T>(T values, string rootName, bool omitXmlDeclaration = false,
bool indentXml = true)
{
string xml = string.Empty;
var serializer = new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
var settings = new XmlWriterSettings()
{
Indent = indentXml,
OmitXmlDeclaration = omitXmlDeclaration
};
XmlSerializerNamespaces @namespace = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
using (var stream = new StringWriter())
using (var writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, values, @namespace);
xml = stream.ToString();
}
return xml;
}
-------------------------------------------------------------------------------------------------------------------------
private static bool IsValid(object entity)
{
System.ComponentModel.DataAnnotations.ValidationContext validatorContext = new System.ComponentModel.DataAnnotations.ValidationContext(entity);
IList<ValidationResult> validationResult = new List<ValidationResult>();
bool isValid = Validator.TryValidateObject(entity, validatorContext, validationResult, true);
return isValid;
}
---------------------------------------------------------------------------------------------------------------------------
XmlSerializer serializer = new XmlSerializer(typeof(ImportPurchase[]), new XmlRootAttribute("Purchases"));
ImportPurchase[] purchasesDto = (ImportPurchase[])serializer.Deserialize(new StringReader(xmlString));