0

I have the following XML that uses the name “Part” in multiple locations. I just want to access the first level elements called “Part” and not for my Linq expression to also pickup the child elements called “Part”. I’ve used the following Linq to accomplish what I want but it seems a bit messy. Can it be improved ?

<Stuff>
  <Parts>
   <Part>
     <A>
       <Part>
         <B>10</B>
       </Part>
    </A>
 </Part>
   <Part>
     <A>
       <Part>
         <B>10</B>
       </Part>
     </A>
  </Part>
 </Parts>
</Stuff>


var pbp = data.Descendants("Part")
            .Where(b => b.Parent == data.Element("Parts"))
            .Select(b => (Part)Deserialise(b.ToString(), typeof(Part)));

return pbp.ToList();

1 Answer 1

1

Would you prefer that form ?

var pbp = from p in data.Element("Parts").Elements("Part")
          select (Part)Deserialise(p.ToString(), typeof(Part));
return pbp.ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.