We are interested in finding maximum number of attributes a node has in a XML document. My code is below using C#:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\ABC.xml");
XmlNode root = xmlDoc.DocumentElement;
int nodeAttrCount = 0;
foreach (XmlNode node in root)
if (nodeAttrCount < node.Attributes.Count)
nodeAttrCount = node.Attributes.Count;
We are interested is: do we have any thing better than this. Like any method or property which give us the same result or anyother option.
You can also use LINQ to XML:
The above code traverses all the xml nodes (it works with nested nodes too) and get the maximum number of attributes.
For .net 2.0:
This is basically the same as your solution;
the main difference is that it considers all nodes at every nesting level (using XPath navigation).