I have a program where the variable week is made up of object array[7] days and days is made up of object array[9] hours. Each hours object has 3 data members. I want to assign each member of a particular day and hour a specific value from an xml file:
<parent>
<day>wednesday
<hour>18.00
<value1>5</value1>
<value2>10</value2>
<value3>15</value3>
</hour>
</day>
<day>thursday
<hour>18.00
<value1>2</value1>
<value2>3</value2>
<value3>9</value3>
</hour>
</day>
</parent>
day[wedensday].hour[18.00].member1 = xml.day[wednesday].hour[18.00].value1
day[wedensday].hour[18.00].member1 = xml.day[wednesday].hour[18.00].value2
day[wedensday].hour[18.00].member1 = xml.day[wednesday].hour[18.00].value3
I am new to working with xml and have come up with the following pseudo code but am having difficultly implementing it:
member[i] = value[i] where day[i].name == xml.day && day[i].hour[i] == xml.hour
If anyone could guide me or direct me to a resource that would help with this problem it would be much appreciated, thanks 🙂
You can parse your xml with Linq to xml:
week will be strongly-typed anonymous object. Usage:
Explanation:
xdoc.Descendants("day")returns all<day>nodes. But we don’t need xml nodes, so we do a projection, by selecting anonymous object, which will represent day. This object has two properties –NameandHours. Name we get from first node of<day>element, which is text node (‘wednesday’ for first day). Hours we get by selecting<hour>elements of<day>and also making projection to anonymous object representing hour. Etc.If you will move day name and hour value to attributes like this:
Then parsing will look like: