如何序列化 class 对象并将其保存到多个 XML 文件
How to Serialize a class object and save it on to Multiple XML files
我有一个员工 class 如下所示
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 从数据库中检索员工数据,如下所示
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 table 包含 536 行,而我的页面大小为 100,那么我应该将其保存在 6 个 XML 文件中,分别包含 100,100,100,100,100 和 36 行。我如何使用 entity framework 实现此目标??
您可以使用 .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();
}
}
过去我用这个 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();
}
我有一个员工 class 如下所示
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 从数据库中检索员工数据,如下所示
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 table 包含 536 行,而我的页面大小为 100,那么我应该将其保存在 6 个 XML 文件中,分别包含 100,100,100,100,100 和 36 行。我如何使用 entity framework 实现此目标??
您可以使用 .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();
}
}
过去我用这个 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();
}