I want to check given node is exist or not in *.xml file.
I try:
string language = node.SelectSingleNode("language") != null ? (node.SelectSingleNode("language").Value == "en" ? "en-US" : "en-US") : "en-US";
But I think its check only for node value.In some xml file I haven’t node called language So its gives a Null Reference Ex...
How to check Given node is exist or not in *.xml file? Thanks.
Something is
null. You are checking the selected “language” node fornull, so isnodeitselfnull?Spread the code out over more lines, nested
?:code is not easy to follow and you have had to repeat default values and function calls.Use variables, such as one for
node.SelectSingleNode("language")so you don’t do that twice. And this will help you find the bug.