0

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.

Liam
  • 27,717
  • 28
  • 128
  • 190
Unapedra
  • 2,043
  • 4
  • 25
  • 42
  • What is the logic behind parent(`_MK`) and child(`_ML`) relation? For example does `_CC` and `_CD` items related? – Fabio Jan 30 '20 at 08:59
  • It's not a parent-child relationship. It's more a complementary information for some specific items. The `_MK` and `_ML` is just an example. The `_CC` and `_CD` are not related between them and, in this case, as there is no `_MK` before them, they would be independent. However, the `_MK` was just a sample. – Unapedra Jan 30 '20 at 09:59

0 Answers0