Linq2XML:获取候选人获胜的元素数量2

时间:2010-03-08 00:55:31

标签: c# linq-to-xml

我有一个XML文档,格式如下

<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>

根据帮助建议,我使用下面的代码给出了以下格式 C#代码:

void Main()
{
    XDocument xmlVectors2 = XDocument.Load(@"C:\\Province_Data.xml"); 
     var elemList= from elem in xmlVectors2.Descendants("Province_Data")
              where elem.Elements().Count() > 1 
              select elem;

    foreach(XElement element in elemList)
    {
      XElement elem1= new XElement("Candidates",
                       new XElement(element.Element("Sam_Kea").Name),
                       new XElement(element.Element("Jeje").Name),
                       new XElement(element.Element("John_Doe").Name)
                       );

       XElement elem2 = new XElement("Provinces",
                       new XAttribute("Province", (string)element.Element("Province").Value),
                       new XAttribute("Registered_Voters",element.Element("Registered_Voters").Value),              

                             new XElement
                                  ("Candidate",
                                  new XAttribute("Name",element.Element("Sam_Kea").Name),
                                  new XAttribute("Votes",element.Element("Sam_Kea").Value)                             
                                  ),
                              new XElement
                                   ("Candidate",
                                  new XAttribute("Name",element.Element("Jeje").Name),
                                  new XAttribute("Votes",element.Element("Jeje").Value)                            
                                  ),
                                   new XElement
                                   ("Candidate",
                                  new XAttribute("Name",element.Element("John_Doe").Name),
                                  new XAttribute("Votes",element.Element("John_Doe").Value)                            
                                  ));

    }
} 

新格式:

<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>

如何在我的代码中使用“foreach”来获取此链接中的结果。 Get the Count of Elements where candidate has won

0 个答案:

没有答案