如何序列化类对象并将其保存到多个XML文件

时间:2018-03-16 12:24:48

标签: c# xml entity-framework serialization xml-serialization

我有一个像下面这样的员工类

 public class Employee
    {
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime DOB { get; set; }
    }

我正在使用Entity Framework从数据库中检索Employee数据,如下所示

using (var dbContext = new EmployeeEntities())
{
    List<Employee> employeeList = new List<Employee>();
    employeeList = dbContext.employee.Select(x => new Employee
                                        {
                                            EmployeeID = x.EmployeeID,
                                            Name = x.Name,
                                            Age = x.Age,
                                            DOB = x.DOB
                                        }).ToList();
}

然后我序列化列表并将其保存为XML文件。

if (employeeList.Count > 0)
{
     XmlSerializer mySerializer = new XmlSerializer(typeof(List<Employee>));
     TextWriter myWriter = new StreamWriter("D:\\Employee.xml", true);
     mySerializer.Serialize(myWriter, employeeList);
     myWriter.Close();
}

我的要求是根据我指定的页面大小将结果集保存到多个XML文件。例如,如果Employee表包含536行,而我的页面大小为100,那么我应该将它保存在分别包含100,100,100,100,100和36行的6个XML文件中。我如何使用实体框架实现这一目标?

2 个答案:

答案 0 :(得分:0)

您可以使用.Skip().Take()来完成这项工作。

.Take()将从列表中获取已定义数量的项目,如果数量较少,则需要全部。 .Skip()将跳过定义的金额。

通过使用tmp变量,您可以关闭employeeList并保存每个文件。

  if (employeeList.Count > 0)
        {
            int take = 100;
            int i = 0;
            while (employeeList.Any())
            {
                i++;
                var tmp = employeeList.Take(take)ToList();
                employeeList = employeeList.Skip(take).ToList();
                XmlSerializer mySerializer = new XmlSerializer(typeof(List<Employee>));
                TextWriter myWriter = new StreamWriter("D:\\Employee"+i+".xml", true);
                mySerializer.Serialize(myWriter, tmp);
                myWriter.Close();
            }
        }

答案 1 :(得分:0)

过去我使用此Batch扩展方法完成了此操作:

public static class EnumerableExtensions
{
    /// <summary> 
    /// Splits the values in an enumerable by position into batches of the specified size 
    /// </summary> 
    public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> items, int batchSize)
    {
        using (var e = items.GetEnumerator())
        {
            while (e.MoveNext()) // check before entering the loop 
            {
                yield return BatchOf(e, batchSize);
            }
        }
    }

    private static IEnumerable<T> BatchOf<T>(IEnumerator<T> e, int batchSize)
    {
        for (var i = 0; i < batchSize; i++)
        {
            if (i > 0 && !e.MoveNext()) // already checked once before entering the loop / so only check on subsequent iterations 
            {
                yield break;
            }
            yield return e.Current;
        }
    }
}

然后使用它很简单:

int i = 0;
foreach (var batch in employeeList.Batch(100))
{
    XmlSerializer mySerializer = new XmlSerializer(typeof(List<Employee>));
    TextWriter myWriter = new StreamWriter($"D:\\Employee{i++}.xml", true);
    mySerializer.Serialize(myWriter, batch.ToList());
    myWriter.Close();
}