我需要将字符串设置为这样的
<Student>
<StudentID></StudentID>
<FirstName></FirstName>
<Fees>
<Fee>
<FeeID></FeeID>
</Fee>
</Fees>
</Student>
<Student>
<StudentID></StudentID>
<FirstName></FirstName>
<Fees>
<Fee>
<FeeID></FeeID>
</Fee>
</Fees>
</Student>
... and so on
我的代码
string strFeeData = ""; // XML Content for Fees
string strStuData = ""; // XML Content for Students with Fees
foreach (var studentData in sorted)
{
foreach(Fee feeData in studentData.Fees)
{
strFeeData = strFeeData + "<Fee>\n\t<FeeID>" + feeData.FeeID + "</FeeID>\n\t<FeeName>" + feeData.FeeName + "</FeeName>\n\t<Amount>" + feeData.Amount + "</Amount>\n\t<DueDate>" + feeData.DueDate + "</DueDate>\n</Fee>\n";
}
strStuData = strStuData + "<Student>\n\t<StudentID>" + studentData.StudentID + "</StudentID>\n<FirstName>" + studentData.FirstName + "</FirstName>\n<LastName>" + studentData.LastName + "</LastName>\n<Grade>" + studentData.Grade + "</Grade>\n<Fees>\n\t" + strFeeData + "</Fees></Student>\n";
}
我正确输出了这部分
<Student>
<StudentID>1</StudentID>
<FirstName>David</FirstName>
...
<Student>
<StudentID>2</StudentID>
<FirstName>John</FirstName>
但要收费
<Fees>
<Fee>
<FeeID>12</FeeID>
....
<Fees>
<Fee>
<FeeID>12</FeeID>
我为每个学生收取相同的费用。
我的双 foreach 循环有什么问题?
(我已经检查过 sorted 的 Fee 有不同的值,问题肯定出在双 foreach 循环上)
谢谢