User904061592 posted
We have a daily XML doc that gets processed everyday and i am trying to implement this using some linq queries to get information and dump into ms sql but have run into a problem. The XML document is made up of thousands of records with 100's of fields
in each record, but not all fields come up in all records. I am trying to query some of these fields and I am getting a Null reference when it comes across a record that does not contain that xml tag. i have included some code below to further
help in the explanation.
XDocument xmlFile = XDocument.Load("c:\\test2.xml");
var query = from c in xmlFile.Elements("daily").Elements("information").Elements("file").Elements("keys").Elements("case")
select new
{
FilingDate = c.Element("case-file-header").Element("filing-date").Value,
RegistrationDate = string.IsNullOrEmpty(c.Element("case-file-header").Element("registration-date").Value) ? c.Element("case-file-header").Element("registration-date").Value : null,
};
I have tried the above with string.empty as well but it does not seem to work. Also to be clear the <registration-date></registration-date> does not exist at all in the xml document for some "case" elements, but does for others. i thought
about using a where clause but i am not sure if it would work and it is not feasible given the number of fields. Any ideas would be greatly appreciated.
Thanks,
Drew