1

我需要将字符串设置为这样的

<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 循环上)

谢谢

4

2 回答 2

2

strFeeData在外循环中用空字符串初始化:

foreach (var studentData in sorted)
{
    strFeeData = "";

    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";
}

但是,使用字符串连接创建 XML 确实是个坏主意!您应该阅读有关序列化或 LINQ to XML 的信息!

LINQ to XML 版本

var xml = sorted.Select(s => new XElement("Student"
                                 new XElement("StudentID", s.StudentID),
                                 new XElement("FirstName", s.FirstName),
                                 new XElement("LastName", s.LastName),
                                 new XElement("Fees",
                                    s.Fees.Select(f => new XElement("Fee",
                                                           new XElement("FeeID", f.FeeID))).ToArray())));
于 2013-03-13T08:02:34.030 回答
2

你为什么不尝试这样,你总是可以使用 XmlSerializer 将 C# 对象列表转换为 XML 文档。

public string ToXML()
    {
        var stringwriter = new System.IO.StringWriter();
        var serializer = new XmlSerializer(this.GetType());
        serializer.Serialize(stringwriter, this);
        return stringwriter.ToString();
    }

这将您的课程序列化为 xml 字符串

另请检查:Convert a C# object to XML?

Linq To XML 方式

XElement _customers = new XElement("Students",
                       from c in objCust
                       orderby c.CustID //descending 
                        select new XElement("Student",
                            new XElement("name", c.StudentID),
                            new XElement("ID", c.FirstName),
                            new XElement("phone", c.LastName),
                            new XElement("Fees", (from x in x.Fees
                                                  orderby x.FeeID//descending 
                            select new XElement("FeeID",x.FeesId)) 
                        ))

                  );
于 2013-03-13T08:04:42.887 回答