计算XML元素值

Im试图对Team Element中的值进行计数时,会抛出所有Xml节点,并以以下格式将其填充到“列表”框中:Team1,Count3。Thnx寻求帮助


    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>


慕森卡
浏览 135回答 2
2回答

交互式爱情

如果我们假设您xml是这样的:<Root><Team name="team1">&nbsp;<Participants>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Participant>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Participant>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Participant>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Participant>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<Participant>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Participant>&nbsp;</Participants></Team><Team name="team2">&nbsp;<Participants>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Participant>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Participant>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Participant>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Participant>&nbsp;</Participants></Team></Root>然后将您的代码更改为:XDocument xdoc = XDocument.Load(@"your xml path");var teams = xdoc.Descendants("Team");foreach (XElement team in teams){&nbsp; &nbsp; var firstElement = team.Elements();//Get all elements, here is only one element&nbsp; &nbsp; var participants = firstElement.Elements();&nbsp; &nbsp; int teamCount = participants.Count();&nbsp; &nbsp; var teamName = team.FirstAttribute.Value;&nbsp; &nbsp; Console.Out.WriteLine("{0} contains {1} member(s)...", teamName, teamCount);&nbsp;}Console.ReadLine();笔记:1-每个xml文件中最多只能有一个根元素,这里是<Root>。2-我认为每个team元素中只有一个元素,所以team.Elements()给您所有元素。3-我想每个team命名name的元素都有一个属性,所以team.FirstAttribute.Value给您元素的nameof team,否则您应该使用类似team.Attributes("name")get属性的方法。
打开App,查看更多内容
随时随地看视频慕课网APP