I want to count attributes in a XML document. Without it works well. But with a comment I got an exception:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
It is possible to count in other way? Or why the count methode don't ignore the comment line in the xml example?
XmlDocument doc = new XmlDocument();
doc.LoadXml(@"
<ListOfMegaCities>
<MegaCities city=""Moscow"" residents=""11503501"" foundation=""1624""/>
<MegaCities residents=""8336817""/> <!-- comment test -->
<MegaCities foundation=""1066"" city=""London""/>
</ListOfMegaCities>");
XmlNodeList elemList = doc.GetElementsByTagName("ListOfMegaCities");
Console.Write("\nCount Attributes: " + elemList[0].ChildNodes[0].Attributes.Count);
Console.Write("\nCount Attributes: " + elemList[0].ChildNodes[1].Attributes.Count);
Console.Write("\nCount Attributes: " + elemList[0].ChildNodes[2].Attributes.Count);