I have something similar to the XML below. I parse it with Linq in C # and it works fine until we get to a product that does not have type .
<productList> <product> <type> <colour>red</colour> </type> <name>First</name> </product> <product> <name>Second</name> </product> </productList>
I am trying to access the colour element in type , but when the code reaches product that does not have a type element, I get an "Object" link that is not set to an instance of the object error.
Here is the code I'm using to access it at the moment. All this was a little complicated with the attempts that I was trying to solve.
productColour = products.Element("type").Descendants().FirstOrDefault() == null ? string.Empty : products.Element("type").Descendants().FirstOrDefault().Value,
I know that the colour element is always the first under type , so I can use .FirstOrDefault() , but still getting the same error using this code.
Can someone point me in the right direction? I also tried casting on a string as well with: ?? " " ?? " " at the end, everything still hasn’t helped.
Edit: Thanks to @ anthony-pegram, it seems like the problem is that I always try to capture the descendants, even if the parent element does not exist. So it seems like I need to check the parent before capturing the child - can anyone point me in the direction for this?
source share