Search code examples
c#xmlxelement

Fail getting specific XElement


I have a problem. I need to loop through a xml and get a specific data. enter image description here

I need to get for each <API-MAPPINGS> all <MAPPED-ARGUMENT-TYPE> and write them into a List. I did like this:

 foreach (var typemapping in maplist.Elements("API- 
 MAPPINGS").Descendants("TYPE-MAP")) 
 {
     foreach (var mappedArguments in maplist.Elements("MAPPED-ARGUMENT-TYPE"))
        {
           newTypeMap.MappedArgumentType.Add(mappedArguments.Value);
        }
 }

But it's not working. Can somebody tell me please what I'm doing wrong?


Solution

  • It appears this may be a simple issue as pointed out by @SebastianHofmann. Inside your second foreach loop, you need to use the variable from the previous loop. See below for the updated loops:

    foreach (var typemapping in maplist.Elements("API-MAPPINGS").Descendants("TYPE-MAP"))
    {
        foreach (var mappedArguments in typemapping.Elements("MAPPED-ARGUMENT-TYPE")) //change this line
        {
            newTypeMap.MappedArgumentType.Add(mappedArguments.Value);
        }
    }