0
我正在將XML文件的內容讀取到IEnumerable集合(數組)中,我需要在單獨的頁面上打印每個迭代(類似XML數據的塊)。重疊打印IEnumerable集合
我正在使用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;
}
Skip不適用於GEmployeeXGD.GetEmployeeGeneralData()。如果按照指示分配(var items = GEmployeeXGD.GetEmployeeGeneralData();),則項目包含所有集合的數組元素(即employeeInfo [0],employeeInfo [1]等)。有沒有辦法索引這些元素,以便foreach每次只使用一個? – David