Hi,
I am using XmlReader to read an XML File and I want to count XML Element right under of Document Element that as I know it should be the root element.
XML
<?xml version="1.0" encoding="utf-8"?>
<NewsLetters>
<EMail Date="10/10/2009">[email protected]</EMail>
<EMail Date="10/10/2009">[email protected]</EMail>
<EMail Date="10/10/2009">[email protected]</EMail>
<EMail Date="10/10/2009">[email protected]</EMail>
</NewsLetters>
C# Code :
public static string TotalMemberCount()
{
XmlTextReader reader = new XmlTextReader(HttpContext.Current.Server.MapPath("~/Newsletter/NewsLetter.xml"));
int totalCount = 0;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.NodeType != XmlNodeType.Document)
totalCount++;
}
return totalCount.ToString();
}
Normally I was expecting 4 records but it returns 5 because it also counts the root element. What should I do ? Actually I know how to solve with using XDocument,XElement and LINQ but I want to solve in this way, don't ask me why because I want to learn every way in which I can solve such problems.
Thanks in advance.
Sincerely....