本文介绍了如何在XML文件中计算节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助计算XML文件中的用户数。示例XML文件可能如下所示
I need help count the number of users in the a XML file. The sample XML file may look like this
< Users>
<Users>
&NBSP;&NBSP; <用户>
<User>
< / User>
</User>
<用户>
<User>
< / User>
</User>
......
......
<用户>
<User>
< / User>
</User>
< / Users>
</Users>
所以我必须算上如何 很多<用户>在XML 文件中?
So I have to count how many <User> in the XML file ?
推荐答案
Hi.Following is the Code Written in the Console Application which will solve your problem.
This Code will Display the Count of the child nodes under the DocumentElement.
using System;
using System.IO;
using System.Xml;
namespace UserCollection
{
class Program
{
static int Main(string[] args)
{
string strFilename = "User.xml";
XmlDocument xmlDoc = new XmlDocument();
if (File.Exists(strFilename))
{
xmlDoc.Load(strFilename);
XmlElement elm = xmlDoc.DocumentElement;
XmlNodeList lstUsers = elm.ChildNodes;
Console.WriteLine("The root element contains {0} nodes",
lstUsers.Count);
}
else
Console.WriteLine("The file {0} could not be located",
strFilename);
Console.WriteLine();
return 0;
}
}
}
这篇关于如何在XML文件中计算节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!