Reputation: 15435
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?
Upvotes: 1
Views: 473
Reputation: 2574
Try this:
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");
xml.WriteStartElement("userPerformance");
xml.WriteElementString("type", "yearly");
// assume that I have the proper allDays as a List of DateTime
foreach (var days in allDays)
{
// What will be my StartElement?
xml.WriteStartElement("values");
xml.WriteElementString("start", DateTime.Now.ToString());
xml.WriteElementString("end", DateTime.Now.ToString());
xml.WriteElementString("value", "0815");
xml.WriteEndElement();
}
xml.WriteEndElement();
xml.WriteEndElement();
}
xml.WriteEndElement();
Upvotes: 1
Reputation: 969
An other way could be to do it this way: (this code is an example which may not compile)
var userProfiles = new XElement("userProfiles");
foreach (var currentProfile in profiles)
{
var profile = new XElement("profile");
profile.Add(new XElement("name", currentProfile.name));
profile.Add(new XElement("id", currentProfile.id));
var performance = new XElement("performance");
performance.Add(new XElement("type", "yearly"));
var values = new XElement("values");
foreach(var days in allDays)
{
var value = new XElement("value");
value.Add(new XElement("start", days.start));
value.Add(new XElement("end", days.end));
value.Add(new XElement("value", days.value));
values.Add(value);
}
performance.Add(values);
profile.Add(performance);
userProfiles.Add(profile);
}
Upvotes: 0
Reputation: 5401
I think the sequence in the XML is the same as the sequence with which you call WriteStartElement. So I think you need to sort the allDays in the way you want them to appear in the XML.
Upvotes: 0