I'm trying to serialize to the following XML structure using the XmlWriter in C#:
<userProfiles>
<groupId>12345</groupId>
<profile>
<name>test 1</name>
<id>10000</id>
<ns:userPerformance>
<ns:type>yearly</ns:type>
<ns:values>
<ns:value>
<ns:start>start time</ns:start>
<ns:end>end time</ns:end>
<ns:value>6712736</ns:value>
</ns:value>
</ns:values>
</ns:userPerformance>
</profile>
<profile>
<name>test 2</name>
<id>20000</id>
<ns:userPerformance>
<ns:type>yearly</ns:type>
<ns:values>
<ns:value>
<ns:start>start time</ns:start>
<ns:end>end time</ns:end>
<ns:value>712367</ns:value>
</ns:value>
<ns:value>
<ns:start>start time</ns:start>
<ns:end>end time</ns:end>
<ns:value>54656</ns:value>
</ns:value>
</ns:values>
</ns:userPerformance>
</profile>
</userProfiles>
I'm trying to populate the userPerformance elements. I know that I have to iterate between two dates to create the ns:values, but how can I set which element would be the start element so that I populate my ns:values? Here is what I have so far!
var xml = new XmlTextWriter(new StringWriter());
xml.WriteStartDocument();
xml.WriteStartElement("userProfiles");
xml.WriteElementString("groupId", "1");
foreach (var profile in profiles)
{
xml.WriteStartElement("profile");
{
xml.WriteElementString("name", "test 1");
xml.WriteElementString("id", "10000");
// assume that I have the proper allDays as a List of DateTime
foreach(var days in allDays)
{
// What will be my StartElement?
xml.WriteStartElement("userPerformance");
}
}
}
If I have userPerformance as the StartElement inside the inner loop where I iterate over the days, I might end up having it multiple times for each of the day. How can I avoid it?