1

I need to set a string into something like this

<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

My code

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

I got this part outputted correctly

<Student>
    <StudentID>1</StudentID>
    <FirstName>David</FirstName>
...
<Student>
    <StudentID>2</StudentID>
    <FirstName>John</FirstName>

But for Fees

<Fees>
     <Fee>
         <FeeID>12</FeeID>
....
<Fees>
     <Fee>
         <FeeID>12</FeeID>

I got the same Fees for every students.

What is wrong with my double foreach loop?

(I already checked sorted's Fee has different values, the problem must be at the double foreach loop)

Thanks

3
  • 1
    Why are you creating XML by hand? It's an incredibly error-prone approach. Use one of the many XML APIs available in .NET - ideally LINQ to XML. Commented Mar 13, 2013 at 8:03
  • I am still beginner in C# >< Commented Mar 13, 2013 at 8:08
  • that was the only solution I can think of Commented Mar 13, 2013 at 8:09

2 Answers 2

2

Initialize strFeeData with empty string within outer loop:

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

However, it's really bad idea to create XML using string concatenation! You should read about Serialization or LINQ to XML!

LINQ to XML version

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())));
Sign up to request clarification or add additional context in comments.

4 Comments

with the strFeeData = ""; it made it worse :p now it shows only 1 Student with all the fees
With the LINQ to XML version, once I have the var xml, do I just textwriter(xml); ? or what to do with it?
I ToString(); it and tried to console.writeline, it ended up showing "System.Linq.Enumerable+WhereSelectEnumerableIterator2[<>f__AnonymousType06[Sys tem.Int32,System.String,System.String,System.String,System.Double,System.Linq.IO rderedEnumerable`1[StudentList.RServiceReference.Fee]],System.Xml.Linq.XElement]"
The main problem here is that you don't have a root element at all within your XML! You should create node like <Students> with all students (var students = new XElement("Students", xml);), and then call students.ToString()
2

why dont you try out like this ,You can always use XmlSerializer to transform a list of C# objects to XML document.

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

this seializ your class to xml string

Also check this : Convert a C# object to XML?

Linq To XML way

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)) 
                        ))

                  );

1 Comment

@user1618180 - check the serialization in C# on google and also check the link given you will get idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.