Sometimes a parent node doesn't have a child node xml xpath

  • Thread starter Thread starter CuriousCoder15
  • Start date Start date
C

CuriousCoder15

Guest
Hi,

I am reading an xml file using XDocument and XPath, in my query I am reading like so:


var elements = doc.XPathSelectElements("something/somethingelse");
var items = from e in elements
select new
{
name = e.Attribute("name")?.Value,
age = e.Attrubute("age")?.Value,
details = from d in e.XPathSelectElements("info")
select new
{
status = d.Attribute("statusReport")?.Value,
number =
d.XPathSelectElement("shirt").Attribute("number")?.Value,

}
}

After I foreach loop through the 'items' and 'details' collections.



if the statusReport = "inActive" there will not be a xpathselectElement("shirt") or a .Attribute("number") and I get the object not set to an instance of an object exception. How can I prevent this happening?


Thank you for your help.


CuriousCoder

Continue reading...
 
Back
Top