Friday 16 December 2011

XmlReader to read an XML File and Document Element

XML
<?xml version="1.0" encoding="utf-8"?>
<NewsLetters Root="True">
  <EMail Date="10/10/2009">hello@hello.com</EMail>
  <EMail Date="10/10/2009">hello@hello.com</EMail>
  <EMail Date="10/10/2009">hello@hello.com</EMail>
  <EMail Date="10/10/2009">hello@hello.com</EMail>
</NewsLetters>

C#

public static string TotalMemberCount()
{
int totalCount = 0;
using (XmlTextReader reader = new XmlTextReader(HttpContext.Current.Server.MapPath("~/Newsletter/NewsLetter.xml")))
{
while (reader.Read())
{
if (reader.NodeType != XmlNodeType.XmlDeclaration && reader.NodeType == XmlNodeType.Element)
{
if (reader.MoveToFirstAttribute())
{
if (reader.Value == "True")
//gotcha, I don't want this,this is root element
continue;
}
totalCount++;
}
}
return totalCount.ToString();
}


}

No comments:

Post a Comment