I've got some data that comes in XML format, and which contains a list of items:
<message>
<items id="_DD" value="28OCT"/>
<items id="_PC" value="ABV"/>
<items id="_CT" value="15H03"/>
<items id="_CD" value="31OCT"/>
<items id="_AN" value="5556695"/>
<items id="_CC" value="PSD"/>
<items id="_MK" value="OJJ"/>
<items id="_DT" value="LKM"/>
<items id="_IT" value="B239"/>
<items id="_ML" value="4569"/>
<items id="_TA" value=""/>
<items id="_P1" value=""/>
<items id="_MK" value="APK"/>
<items id="_DT" value="MMK"/>
<items id="_IT" value="B219"/>
<items id="_ML" value="0069"/>
<items id="_RC" value="0"/>
...
</message>
Most of the items are independent between them, but there can be cases in which they are related. For example, the id="_MK"
items mark a new category.
So I need to make a list of elements (with id="_ML"
, for example) and be able to get, also, the category to which they belong (which is the value of the previous item with id="_MK"
).
How do I get the value, for each item, of their previous id="_MK"
sibling, so I can assign the category (the value of that id="_MK"
item) to them?
In the case of the "_ML"
list, the result should contain:
[
{
value: "4569",
category: "OJJ"
},
{
value: "0069",
category: "APK"
}
]
I know I could do this with a foreach
, but the list of items is quite large and I'm worried about performance. I haven't been able to find any solution using LinQ, which was my best option in mind.
Also, the format and structure comes from a third party source, so I cannot change it.