Nettan Nida

Nettan Nida

  • NA
  • 4
  • 3.4k

Count people in XML using C#

Oct 31 2013 12:16 PM
I need some help with C# and XML.
I'm building a web application and need to present information about number of peope with gender Male in a specific department (How many men in Dep1).

This is my XML-file:
  <company>
  <department>
  <departmentname>Dep 1</departmentname>
  <people>
  <person>
  <name>Sean</name>
  <date>2013-10-10</date>
  <gender>male</gender>
  <age>40</age>
  </person>
  <person>
  <name>John</name>
  <date>2013-10-18</date>
  <gender>male</gender>
  <age>45</age>
  </person>
  <person>
  <name>Linda</name>
  <date>2013-09-10</date>
  <gender>female</gender>
  <age>42</age>
  </person>
  <person>
  <name>Bob</name>
  <date>2013-10-01</date>
  <gender>male</gender>
  <age>35</age>
  </person>
  </people>
  </department>
  <department>
  <departmentname>Dep 2</departmentname>
  <people>
  <person>
  <name>Art</name>
  <date>2013-09-10</date>
  <gender>male</gender>
  <age>38</age>
  </person>
  <person>
  <name>Christina</name>
  <date>2013-10-20</date>
  <gender>female</gender>
  <age>45</age>
  </person>
  <person>
  <name>Marie</name>
  <date>2013-09-10</date>
  <gender>female</gender>
  <age>49</age>
  </person>
  </people>
  </department>
  </company>
My code (not finished and not working):
  XElement company= XElement.Load(Server.MapPath("myXML.xml"));
 
  XElement department= (from p in company.Elements("department")
  where p.Element("departmentname").Value == 'Dep 1' && p.Element
  ("gender").Value == 'male'
  select p).Count(); 
 
  numberTextBox.Text = department.Element; //???How to write to textbox

Answers (1)