本文介绍了获取元素的伯爵那里的候选人赢得了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LINQ2Xml:我想获得元素,其中的候选人赢得了在每个省的数量。我需要一些帮助。

 < Pronvice_Data>
    < Pronvice> PronviceA< / Pronvice>
    < Registered_Voters> 115852< / Registered_Voters>
    &其中; Sam_Kea> 100℃/ Sam_Kea>
    &其中;杰杰奥> 500℃; /杰杰奥>
    &其中; john_doe即> 400℃/ john_doe即>
< / Pronvice_Data>

< Pronvice_Data>
    < Pronvice> PronviceA< / Pronvice>
    < Registered_Voters> 25852< / Registered_Voters>
    &其中; Sam_Kea> 200℃/ Sam_Kea>
    &其中;杰杰奥> 100℃/杰杰奥>
    &其中; john_doe即大于300&所述; / john_doe即>
< / Pronvice_Data>

< Pronvice_Data>
    < Pronvice> PronviceC< / Pronvice>
    < Registered_Voters> 317684< / Registered_Voters>
    &其中; Sam_Kea> 1000℃; / Sam_Kea>
    <杰杰奥> 1200 LT; /杰杰奥>
    &其中; john_doe即> 190℃; / john_doe即>
< / Pronvice_Data>
 

预期成果:

 候选人|赢得
杰杰奥2
john_doe即1
Sam_Kea 0
 

解决方案

OK,我不喜欢的数据格式,所以我改成了这样:

 <根>
  <考生>
    < Sam_Kea />
    <杰杰奥/>
    < john_doe即/>
  < /考生>

  <各省>
    <省名='ProvinceA'Registered_Voters ='115852'>
      <考生姓名='Sam_Kea的投票=100/>
      <考生姓名='杰杰奥的投票='500'/>
      <考生姓名='john_doe即'票=400/>
    < /省>

    <省名='ProvinceB'Registered_Voters ='25852'>
      <考生姓名='Sam_Kea的投票=200/>
      <考生姓名='杰杰奥的投票=100/>
      <考生姓名='john_doe即'票=300/>
    < /省>

    <省名='ProvinceC'Registered_Voters ='317684'>
      <考生姓名='Sam_Kea的投票=1000/>
      <考生姓名='杰杰奥的投票=1200/>
      <考生姓名='john_doe即'票='190'/>
    < /省>
  < /省>

< /根>
 

这是LINQ到XML code我用:

 公共无效的run()
{
    字符串fileToLoad = this.GetType()名+的.xml。

    的XElement根= XElement.Load(fileToLoad);

    // ================================================ =======
    的System.Console.WriteLine(\ nCandidates:);
    在root.Element(申请人)VAR allCandidates =从C。元素()
        选择c.Name;

    的foreach(VAR d在allCandidates)
        Console.WriteLine({0},d.ToString());


    // ================================================ =======
    的System.Console.WriteLine(在每个省考生的\ n已接收:);

    VAR S1 =从p在root.Element(省)。元素()
        选择新
            {
                箴=(字符串)p.Attribute(姓名),
                NumCandidates = p.Elements(候选人)。COUNT()
            };

    的foreach(VAR d在S1)
        Console.WriteLine({0},d.ToString());


    // ================================================ =======
    的System.Console.WriteLine(\ nCandidate与多数选票:);
    VAR S2 =从p在root.Element(省)。元素()
        让maxVotes =(从C在p.Elements(候选人)选择C)
                       的.max(X =>((INT)x.Attribute(内容)))

        选择新
            {
                箴=(字符串)p.Attribute(姓名),
                选民=(INT)p.Attribute(Registered_Voters),
                候选=(从C在p.Elements(候选人)
                             选择C)。凡(X =>((INT)x.Attribute(内容))== maxVotes)
                    。首先()。属性(名称)。值
            };

    的foreach(VAR d在S2)
        Console.WriteLine({0},d.ToString());


    // ================================================ =======
    的System.Console.WriteLine(\ nCandidates和省#荣获:);

    VAR S4 =从能在allCandidates
        让数=(从对在S2,其中p.Candidate ==可以选择P).Count之间的()
        排序依据计数下降
        选择新{候选人=可以,NumberOfProvincesWon =计数};

    的foreach(VAR d在S4)
        Console.WriteLine({0},d.ToString());
}
 

输出:

 考生:
  Sam_Kea
  杰杰奥
  john_doe即

在每个省考生数:
  {箴= ProvinceA,NumCandidates = 3}
  {箴= ProvinceB,NumCandidates = 3}
  {箴= ProvinceC,NumCandidates = 3}

与候选人得票最多:
  {箴= ProvinceA,投票人= 115852,候选人=杰杰奥}
  {箴= ProvinceB,投票人= 25852,候选人= john_doe即}
  {箴= ProvinceC,投票人= 317684,候选人=杰杰奥}

考生和省#荣获:
  {候选人=杰杰奥,NumberOfProvincesWon = 2}
  {候选人= john_doe即NumberOfProvincesWon = 1}
  {候选人= Sam_Kea,NumberOfProvincesWon = 0}
 

LINQ2Xml:I would like to get the count of elements where candidate has won in every province. I need some help.

<Pronvice_Data>
    <Pronvice>PronviceA</Pronvice>
    <Registered_Voters>115852</Registered_Voters>
    <Sam_Kea>100</Sam_Kea>
    <Jeje>500</Jeje>
    <John_Doe>400</John_Doe>
</Pronvice_Data>

<Pronvice_Data>
    <Pronvice>PronviceA</Pronvice>
    <Registered_Voters>25852</Registered_Voters>
    <Sam_Kea>200</Sam_Kea>
    <Jeje>100</Jeje>
    <John_Doe>300</John_Doe>
</Pronvice_Data>

<Pronvice_Data>
    <Pronvice>PronviceC</Pronvice>
    <Registered_Voters>317684</Registered_Voters>
    <Sam_Kea>1000</Sam_Kea>
    <Jeje>1200</Jeje>
    <John_Doe>190</John_Doe>
</Pronvice_Data>

Expected Result:

Candidate | Won In
Jeje         2
John_Doe     1
Sam_Kea      0
解决方案

OK, I didn't like the data format, so I changed it to this:

<root>
  <Candidates>
    <Sam_Kea/>
    <Jeje/>
    <John_Doe/>
  </Candidates>

  <Provinces>
    <Province name='ProvinceA' Registered_Voters='115852'>
      <Candidate name='Sam_Kea' votes='100'/>
      <Candidate name='Jeje' votes='500'/>
      <Candidate name='John_Doe' votes='400'/>
    </Province>

    <Province name='ProvinceB' Registered_Voters='25852'>
      <Candidate name='Sam_Kea' votes='200'/>
      <Candidate name='Jeje' votes='100'/>
      <Candidate name='John_Doe' votes='300'/>
    </Province>

    <Province name='ProvinceC' Registered_Voters='317684'>
      <Candidate name='Sam_Kea' votes='1000'/>
      <Candidate name='Jeje' votes='1200'/>
      <Candidate name='John_Doe' votes='190'/>
    </Province>
  </Provinces>

</root>

And this is the LINQ-to-XML code I used:

public void Run()
{
    string fileToLoad = this.GetType().Name + ".xml";

    XElement root = XElement.Load(fileToLoad);

    // =======================================================
    System.Console.WriteLine("\nCandidates:");
    var allCandidates = from c in root.Element("Candidates").Elements()
        select c.Name;

    foreach (var d in allCandidates)
        Console.WriteLine("  {0}", d.ToString());


    // =======================================================
    System.Console.WriteLine("\nNumber of Candidates in each Province:");

    var s1 = from p in root.Element("Provinces").Elements()
        select new
            {
                Prov = (string) p.Attribute("name"),
                NumCandidates = p.Elements("Candidate").Count()
            };

    foreach (var d in s1)
        Console.WriteLine("  {0}", d.ToString());


    // =======================================================
    System.Console.WriteLine("\nCandidate with most votes:");
    var s2 = from p in root.Element("Provinces").Elements()
        let maxVotes = (from c in p.Elements("Candidate") select c)
                       .Max(x => ((int)x.Attribute("votes")))

        select new
            {
                Prov = (string) p.Attribute("name"),
                Voters = (int) p.Attribute("Registered_Voters"),
                Candidate = (from c in p.Elements("Candidate")
                             select c).Where(x => ((int)x.Attribute("votes")) == maxVotes)
                    .First().Attribute("name").Value
            };

    foreach (var d in s2)
        Console.WriteLine("  {0}", d.ToString());


    // =======================================================
    System.Console.WriteLine("\nCandidates and the # of provinces won:");

    var s4 = from can in allCandidates
        let count = (from p in s2 where p.Candidate == can select p).Count()
        orderby count descending
        select new { Candidate = can, NumberOfProvincesWon = count };

    foreach (var d in s4)
        Console.WriteLine("  {0}", d.ToString());
}

Output:

Candidates:
  Sam_Kea
  Jeje
  John_Doe

Number of Candidates in each Province:
  { Prov = ProvinceA, NumCandidates = 3 }
  { Prov = ProvinceB, NumCandidates = 3 }
  { Prov = ProvinceC, NumCandidates = 3 }

Candidate with most votes:
  { Prov = ProvinceA, Voters = 115852, Candidate = Jeje }
  { Prov = ProvinceB, Voters = 25852, Candidate = John_Doe }
  { Prov = ProvinceC, Voters = 317684, Candidate = Jeje }

Candidates and the # of provinces won:
  { Candidate = Jeje, NumberOfProvincesWon = 2 }
  { Candidate = John_Doe, NumberOfProvincesWon = 1 }
  { Candidate = Sam_Kea, NumberOfProvincesWon = 0 }

这篇关于获取元素的伯爵那里的候选人赢得了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 14:09