Question : c# linq to xml

I have the following code, and when I try to do the first block, When I get to the loop and debug statement, I get a nullreference error on the select new {name = x.Element("ows_Title").Value}; line, but only during the loop.  The second piece of code works, and will loop 4 times (the correct number of items.  I don't understand why this would happen since I check for null above.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
#if true
            var listItems = from x in results.Descendants(ns + "row")
                            where x.Attribute("ows_Title") != null
                            select new {name = x.Element("ows_Title").Value};
            
            foreach (var item in listItems)
            {

                System.Diagnostics.Debug.Print(item.name);

            }
#else
            var listItems = from x in results.Descendants(ns + "row")
                            where x.Attribute("ows_Title") != null
                            select new { name = "test" };

            foreach (var item in listItems)
            {

                System.Diagnostics.Debug.Print(item.name);

            }

#endif

Answer : c# linq to xml

try:

openFileDialog1.Filter = "Excel files (*.xlsx, *.xlsm)|*.xlsx;*.xlsm";
Random Solutions  
 
programming4us programming4us