tags:

views:

57

answers:

0

I had an XML Document in the format below

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

As suggested by help here,I used the code below to give me this format below C# Code:

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

    }
} 

New Format:

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

How can i use the "foreach" in in my code to get the result in this link. http://stackoverflow.com/questions/2396203/get-the-count-of-elements-where-candidate-has-won