Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI I need to parse dynamic xml with the help of linq. i am not sure whether a pecific element is coming or not but i need to write code for it.
If any element is not present in xml i have get null reference exception.
Can any body tell me how to overcome this problem.
Posted

I think it is worth to look at Linq to XML --> NULL values[^] discussion.

I hope this helps you well.
 
Share this answer
 
Comments
Kim Togo 24-May-11 7:20am    
Good link to MSDN Forum and examples on LINQ and test for null values.
Wonde Tadesse 24-May-11 7:33am    
Thanks Kim
You can alway test for null on a XElement.

C#
var result = from book in books.Elements("book")
             where book.Attribute("id") != null && book.Attribute("name") != null
             select new { Name = book.Attribute("name").Value, ISBN = book.Attribute("isbn") == null ? "Missing" : book.Attribute("isbn").Value };


If attribute "isbn" is missing, the string "Missing" is used.
 
Share this answer
 
v2
You're trying to call method or property on the class instance which was never initialized or explicitly set to null. That's all. I can be anywhere.

Did you run it under Debugger. It would show you exact line of code where it happens. If you still cannot figure out how to fix the problem, post a relevant part of code; don't forget to indicate the lines of code responsible for exception with full exception information.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900