【发布时间】:2017-12-17 03:04:28
【问题描述】:
我正在将 XML 文件的内容读入 IEnumerable 集合(数组),我需要在单独的页面上打印每个迭代(类似 XML 数据的块)。
我正在使用 Print() 函数和 e.HasMorePages。我的问题是 foreach 循环遍历每次打印的 IEnumerable 集合的所有迭代,因此我打印了正确的页数,但每页包含所有迭代,而不是每页一个。谁能给我一个解决方案或更好的方法来管理这个过程的想法?
这是代码的相关部分...
// Print Employee General info
foreach (EmployeeInfo itm in GEmployeeXGD.GetEmployeeGeneralData())
{
try
{
empFirstName = itm.FirstName;
empLastName = itm.LastName;
empMidInitial = itm.MidInitial;
etc…
// Set field coordinates for each employee
// ******* Employee's general information ********
PointF empFirstNameLoc = new PointF(430, 271);
PointF empLastNameLoc = new PointF(600, 271);
PointF empMidInitialLoc = new PointF(563, 271);
etc…
// Send field text data
using (Font courierFont = new Font("Courier", 10, FontStyle.Bold))
{
e.Graphics.DrawString(empFirstName, courierFont, Brushes.Black, empFirstNameLoc);
e.Graphics.DrawString(empLastName, courierFont, Brushes.Black, empLastNameLoc);
e.Graphics.DrawString(empMidInitial, courierFont, Brushes.Black, empMidInitialLoc);
etc…
}
}
catch (Exception error) { MessageBox.Show(error.ToString()); }
e.HasMorePages = (records < Globals.totalRecordCount);
}
这对乔尔很有帮助,谢谢。
GEmployeeXGD 尊重具有单一方法的类。该方法读取我需要的 XML 数据并将 IEnumerable 集合填充为数组。方法是这样的。。
public Array GetEmployeeGeneralData()
{
// XML source file
var xmlEmployeeFile = File.ReadAllText("Corrections.xml");
XDocument employeeDoc = XDocument.Parse(xmlEmployeeFile);
XElement w2cEmployeeDat = employeeDoc.Element("CorrectedDAta");
EmployeeInfo[] employeeGenInfo = null;
if (w2cEmployeeDat != null)
{
IEnumerable<XElement> employeeRecords = w2cEmployeeDat.Elements("Employee");
try
{
employeeGenInfo = (from itm in employeeRecords
select new EmployeeInfo()
{
FirstName = (itm.Element("FirstName") != null) ? itm.Element("FirstName").Value : string.Empty,
LastName = (itm.Element("LastName") != null) ? itm.Element("LastName").Value : string.Empty,
MidInitial = (itm.Element("MidInitial") != null) ? itm.Element("MidInitial").Value : string.Empty,
etc…
}).ToArray<EmployeeInfo>();
}
catch (Exception) { MessageBox.Show(error.ToString()); }
}
Globals.SetRecordCount(employeeGenInfo.Count<EmployeeInfo>());
recordCount = Globals.totalRecordCount;
return employeeGenInfo;
}
【问题讨论】:
标签: c# printing ienumerable