0

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);
Aleksej
  • 49
  • 7
  • You can use the debugger to inspect which of the objects is null. I bet it is `elemList[0].ChildNodes[2].Attributes`. Also comments are nodes. – Klaus Gütter May 21 '21 at 05:33
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Klaus Gütter May 21 '21 at 05:34

3 Answers3

1

try this.

            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");
            foreach(XmlNode childNode in elemList[0].ChildNodes)
            {
                if(childNode.NodeType != XmlNodeType.Comment)
                    Console.Write("\nCount Attributes: " + childNode.Attributes.Count);
            }
Binod Mahto
  • 340
  • 2
  • 13
1

It is better to use LINQ to XML API. It is available in the .Net Framework since 2007.

One single statement gives the answer without any loops.

c#

void Main()
{
    XDocument xdoc = XDocument.Parse(@"<ListOfMegaCities>
               <MegaCities city='Moscow' residents='11503501' foundation='1624'/>
               <MegaCities residents='8336817'/> <!-- comment test -->
               <MegaCities foundation='1066'  city='London'/>
           </ListOfMegaCities>");
    
    int AttributeCounter = xdoc.Descendants("MegaCities")
        .Attributes().Count();
        
    Console.WriteLine("Total attributes counter: {0}", AttributeCounter);
}

Output

Total attributes counter: 6
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
  • Thanks. Yes. I read about the advantages with XML LINQ. Now I rewrite my code and try to use LINQ. But it is very hard for me... I try my best to learn the right way. ;) – Aleksej May 22 '21 at 19:09
0

Ok. I understand comments are also nodes. More exception detailes:

enter image description here

I will try the solution above.

Aleksej
  • 49
  • 7