Linq query of xml feed RRS feed

  • Question

  • 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.



    Wednesday, November 18, 2009 9:05 AM


  • User1716267170 posted

    Hi lbaggz,

    Since there're lots of data in xml document, I guess you may have to check the elements degree again and ensure the LINQ to XML query is correct. Also, from the error message, we may guess it may be caused by it. For a basic LINQ to XML details, I suggest you to refer this article:


    If you still can't find out the problem, please show us the exact error message and xml structure.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 20, 2009 12:32 AM