计数 XML 个元素值
Count XML element Value
我正在尝试计算团队元素中的值抛出所有 Xml 节点并将其填充到以下格式的列表框中:团队 1,计数 3。
感谢帮助
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Team");
foreach (XmlNode xNode in nodeList)
{
foreach (var item in xNode)
{
lstRegPartiData.Items.Add(xNode.InnerText + item);
}
}
我在这里尝试按值计算元素。
这是我的 xml 模板。
<Participants>
<Participant>
<DateTime>21.04.2018</DateTime>
<FullName>N1</FullName>
<Email>email@gmail.com</Email>
<Phone>123456789</Phone>
<DateOfBirth>00/00/1988</DateOfBirth>
<Team>Team1</Team>
<MainWeapon>T1</MainWeapon>
<MainWeaponChrono>120-130 m\s</MainWeaponChrono>
<WeaponClass>C1</WeaponClass>
<Comment>Hello World</Comment>
</Participant>
<Participant>
<DateTime>21.04.2018</DateTime>
<FullName>N1</FullName>
<Email>email@gmail.com</Email>
<Phone>987654321</Phone>
<DateOfBirth>00/00/1988</DateOfBirth>
<Team>Team1</Team>
<MainWeapon>T1</MainWeapon>
<MainWeaponChrono>120-130 m\s</MainWeaponChrono>
<WeaponClass>C1</WeaponClass>
<Comment>My Comment</Comment>
</Participant>
<Participant>
<DateTime>21.04.2018</DateTime>
<FullName>N1</FullName>
<Email>email@gmail.com</Email>
<Phone>123456789</Phone>
<DateOfBirth>00/00/1988</DateOfBirth>
<Team>Team1</Team>
<MainWeapon>T1</MainWeapon>
<MainWeaponChrono>120-130 m\s</MainWeaponChrono>
<WeaponClass>C1</WeaponClass>
<Comment>Hello World</Comment>
</Participant>
</Participants>
如果我们假设您的 xml
类似于:
<Root>
<Team name="team1">
<Participants>
<Participant>
</Participant>
<Participant>
</Participant>
<Participant>
</Participant>
</Participants>
</Team>
<Team name="team2">
<Participants>
<Participant>
</Participant>
<Participant>
</Participant>
</Participants>
</Team>
</Root>
然后将您的代码更改为:
XDocument xdoc = XDocument.Load(@"your xml path");
var teams = xdoc.Descendants("Team");
foreach (XElement team in teams)
{
var firstElement = team.Elements();//Get all elements, here is only one element
var participants = firstElement.Elements();
int teamCount = participants.Count();
var teamName = team.FirstAttribute.Value;
Console.Out.WriteLine("{0} contains {1} member(s)...", teamName, teamCount);
}
Console.ReadLine();
备注:
1-每个xml
文件必须有一个根元素不能多,这里是<Root>
.
2- 我假设每个 team
元素中有一个元素,所以 team.Elements()
给你所有元素。
3- 我假设每个 team
元素都有一个名为 name
的属性,所以 team.FirstAttribute.Value
给你 team
元素的 name
,否则你应该使用类似 team.Attributes("name")
的东西来获取属性。
也许你可以这样做
XmlDocument readDoc = new XmlDocument();
readDoc.Load(@"XML.xml");
var Node = readDoc.SelectNodes("Participants/Participant/Team");
Dictionary<string, int> teamList = new Dictionary<string, int>();
foreach (XmlNode n in Node)
{
if (teamList.ContainsKey(n.InnerText))
teamList[n.InnerText]++;
else
teamList.Add(n.InnerText, 1);
}
我知道它看起来不太好,但它应该可以工作
我正在尝试计算团队元素中的值抛出所有 Xml 节点并将其填充到以下格式的列表框中:团队 1,计数 3。 感谢帮助
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Team");
foreach (XmlNode xNode in nodeList)
{
foreach (var item in xNode)
{
lstRegPartiData.Items.Add(xNode.InnerText + item);
}
}
我在这里尝试按值计算元素。
这是我的 xml 模板。
<Participants>
<Participant>
<DateTime>21.04.2018</DateTime>
<FullName>N1</FullName>
<Email>email@gmail.com</Email>
<Phone>123456789</Phone>
<DateOfBirth>00/00/1988</DateOfBirth>
<Team>Team1</Team>
<MainWeapon>T1</MainWeapon>
<MainWeaponChrono>120-130 m\s</MainWeaponChrono>
<WeaponClass>C1</WeaponClass>
<Comment>Hello World</Comment>
</Participant>
<Participant>
<DateTime>21.04.2018</DateTime>
<FullName>N1</FullName>
<Email>email@gmail.com</Email>
<Phone>987654321</Phone>
<DateOfBirth>00/00/1988</DateOfBirth>
<Team>Team1</Team>
<MainWeapon>T1</MainWeapon>
<MainWeaponChrono>120-130 m\s</MainWeaponChrono>
<WeaponClass>C1</WeaponClass>
<Comment>My Comment</Comment>
</Participant>
<Participant>
<DateTime>21.04.2018</DateTime>
<FullName>N1</FullName>
<Email>email@gmail.com</Email>
<Phone>123456789</Phone>
<DateOfBirth>00/00/1988</DateOfBirth>
<Team>Team1</Team>
<MainWeapon>T1</MainWeapon>
<MainWeaponChrono>120-130 m\s</MainWeaponChrono>
<WeaponClass>C1</WeaponClass>
<Comment>Hello World</Comment>
</Participant>
</Participants>
如果我们假设您的 xml
类似于:
<Root>
<Team name="team1">
<Participants>
<Participant>
</Participant>
<Participant>
</Participant>
<Participant>
</Participant>
</Participants>
</Team>
<Team name="team2">
<Participants>
<Participant>
</Participant>
<Participant>
</Participant>
</Participants>
</Team>
</Root>
然后将您的代码更改为:
XDocument xdoc = XDocument.Load(@"your xml path");
var teams = xdoc.Descendants("Team");
foreach (XElement team in teams)
{
var firstElement = team.Elements();//Get all elements, here is only one element
var participants = firstElement.Elements();
int teamCount = participants.Count();
var teamName = team.FirstAttribute.Value;
Console.Out.WriteLine("{0} contains {1} member(s)...", teamName, teamCount);
}
Console.ReadLine();
备注:
1-每个xml
文件必须有一个根元素不能多,这里是<Root>
.
2- 我假设每个 team
元素中有一个元素,所以 team.Elements()
给你所有元素。
3- 我假设每个 team
元素都有一个名为 name
的属性,所以 team.FirstAttribute.Value
给你 team
元素的 name
,否则你应该使用类似 team.Attributes("name")
的东西来获取属性。
也许你可以这样做
XmlDocument readDoc = new XmlDocument();
readDoc.Load(@"XML.xml");
var Node = readDoc.SelectNodes("Participants/Participant/Team");
Dictionary<string, int> teamList = new Dictionary<string, int>();
foreach (XmlNode n in Node)
{
if (teamList.ContainsKey(n.InnerText))
teamList[n.InnerText]++;
else
teamList.Add(n.InnerText, 1);
}
我知道它看起来不太好,但它应该可以工作