Get all values and group them according to parent node xml

Good day. I would like to ask how can I get the values for each container without including the same element on other container? See my dummy xml file below:
image

What I want is to get the containerinfo values and group them according to their parent container element. I tried to implement it but it always get other containerinfo values on different container element. So the result that I want is this:
1st container:
container1, 5000kgs, 20ft
container2, 10000kgs, 15ft
2nd container:
container3, 7000kgs, 20ft
container4, 8000kgs, 15ft
I hope that someone will explain it in details and with step-by-step solution coz it’s been a week since and still I cannot find a solution. Thank you.

@Bruskie143

  1. First deserialize the xml and say stored in xmlout variable
  2. Use xmlout.Root.Descendants(ifnamspacespresnetaddhere + "Container").ToList
  3. Use for loop with output of step 2 which is of type list of xelement
  4. Inside loop use currentitem.Descendants("ContainerInfo").ToList will give the list of containerinfo again here also you need to add namspace if present
  5. Inside the first loop use second loop for output of step4
  6. Inside the second use currenitem2.element("name").ToString

Hope this helps…if not provide a file of xml can give some more details

Cheers

1 Like

we assume the following:

  • no XML namespaces are involved
  • the XML is a snippet (as it doesn’t have a root element)

For starting / more exploration you can do:

Modelling:

Details For each container:
grafik
xDoc.Root.Descendants(“container”)
Log: "Processing: Container with Num: " + item.Element("containernum").Value

Details For each info
grafik
item.Descendants(“containerinfo”)
Log:

String.Format("{0}, {1}.{2} ",info.Element("name").Value, info.Element("weight").Value, info.Element("height").Value)

It will produce the following output:
grafik

Depending on your further processing demands we can also modify / adapt e.g. with the usage of LINQ like:

grafik

When XML Namespaces are involved habe a look here:

1 Like

Thank you so much for this.

1 Like

Thank you so much for this very detailed step by step. Much appreciated.

Perfect, once it is done please close the topic by:

Thanks

One additional question tho. I cannot access the Element in the info inside the second for each loop (info.Element…)

What might be the cause on this one?

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.