Am developing a web-application in ASP.NET (C#) and following code snippet is part of a function:
string[] paramList = new string[2];
string[] paramValList = new string[2];
XDocument doc = XDocument.Parse(paramXML);
var oParamCollection = from parameters in doc.Descendants("myfunction")
select new
{
Name = parameters.Attribute("myParam").Value,
Value = parameters.Attribute("myParamValue").Value
};
int i=0;
foreach (var oParam in oParamCollection)
{
string name = oParam.Name.Trim();
string value = oParam.Value.Trim();
paramList[i] = oParam.Name.Trim();
paramValList[i++] = oParam.Value.Trim();
if (i >= 2)
break;
}
Following is the value of paramXML variable (parameter) being used at the 3 code-line:
<root><myfunction myparam=" DeliveryDate" myparamvalue="2012-07-01" ></myfunction></root>
Now the issue is that when I open my web-application in Chrome or Firefox, there is not error but when I use it in IE 9 (in same state and with same development environment), I get System.NullReferenceException: Object reference not set to an instance of an object at the start of foreach loop. While debugging, I checked the value of oParamCollection in QuickWatch while using in Chrome or FireFox and I get the count as 1 but when I checked the value while using the application in IE 9, the count gives the exception. Am so much confused. Have spent quite sometime on internet searching for this problem but no pointer till now. SO is my last hope. Thank you in advance.
After lot of debugging, finally I have found the solution. The problem is surely in the code snippet (due to my silly mistake) mentioned in the question. The problematic code is as follows:
The issue was that the XML string had the attribute names
myParamandmyParamValuein lowercase whereas here in code I was checking in camel case. The problem got resolved the moment I changed it to ‘myparam’ and ‘myparamvalue’ into lower case in my code. My rectified code is as follows:But my doubt still remains the same, why it happens only for IE and not while using Chrome or FF? I request respectable members of SO to try this and check it on their own.
I would like to thank Jon Skeet for giving me a pointer (+1 for you). @Jon, it my case, the Value property works for attribute. If you have any reference to cases where the Value may not work, please do share it with me. Thank you.