我正在使用 XmlReader 读取 XML 文件,并且我想在 Document Element 正下方计算 XML Element,据我所知它应该是根元素。
XML
<?xml version="1.0" encoding="utf-8"?>
<NewsLetters>
<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()
{
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();
}
通常我期待 4 条记录,但它返回 5,因为它还计算根元素。我该怎么办 ?实际上我知道如何使用 XDocument、XElement 和 LINQ 来解决,但我想以这种方式解决,不要问我为什么,因为我想学习可以解决此类问题的所有方法。
提前致谢。
真挚地....