hi i am new to coding i have an xml file named employees.xml

<employees>
<employee id="1" name="carla weis" budget="100"/>
<employee id="2" name="tom knox" seniorid="1" budget="50"/>
<employee id="3" name="karl rogers" seniorid="1" budget="400"/>
<employee id="4" name="beverly smith" seniorid="2" budget="200"/>
<employee id="5" name="tom stern" seniorid="4" budget="700"/>
<employee id="6" name="eveyln wilson" seniorid="2" budget="350"/>
<employee id="7" name="bruce white" seniorid="2" budget="650"/>
<employee id="8" name="emily borders" seniorid="2" budget="200"/>
<employee id="9" name="marion borders" seniorid="7" budget="100"/>
<employee id="10" name="michelle gates" seniorid="8" budget="200"/>
<employee id="11" name="tania erhardt" seniorid="5" budget="100"/>
</employees>

an employee has a senior and direct budget. the total budget of an employee is the

employee direct budget and all those that report to the employee directly or indirectly

calculate the total budget for each employee and prints out to the console the name and total budget in descending order of total budget.

Looks like homework. What have you tried? Show some code and maybe you'll get some help.

As a start, I suggest looking at the System.Xml.Linq namespace. That will at least get you started on reading the XML into a usable collection. You're probably going to need do some sort of recursion to arrive at your budget figures, since an employee will have children and those children will have children. For example, it appears the entire list will roll up under Carla Weis (id 1). Tom Knox will also be responsible for a majority of the list, and on down the line.

Assuming I did it right, the output of your code should look like this:

carla weis      3050
tom knox        2550
beverly smith   1000
tom stern       800
bruce white     750
karl rogers     400
emily borders   400
eveyln wilson   350
michelle gates  200
marion borders  100
tania erhardt   100
commented: That's true. +9

hi apegram thanks very much for your reply.
i tried this code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;


namespace employeebudget
{
    class Program
    {
        static void Main(string[] args)
        {

            // Create the query
            var emp = from c in XElement.Load("employees.xml").Elements("employee")
                        select c;

            // Execute the query
            foreach (var employee in emp)
            {
                Console.WriteLine(employee);
            }

            //Pause the application
            Console.ReadLine(); 

            }
        }
    }

however when ever i tried to put any filter like where keyword i keep getting errors. samples of linq on msdn are also no help.please understand my knowledge is limited and i would appreciate if you could share your linq query.

regards,

andy

My query is nothing spectacular, there's no filtering involved. It simply reads the XML document into an enumerable of anonymous types.

var query = from employee in document.Descendants("employee")
            select new
            {
                Id = (int)employee.Attribute("id"),
                Name = (string)employee.Attribute("name"),
                Budget = (decimal)employee.Attribute("budget"),
                SeniorId = (int?)employee.Attribute("seniorid")
            };

From there, you have to figure out the budgets. I used a recursive function to handle that. Give it a try and post if you get stuck.

commented: nice +7

Great post apegram.

I'm more of a concrete entities person but LINQ would probably be the better way to go for this task (and a lot less code!) You could also do it this way

[XmlRoot("employees")]
    public class EmployeeList : List<Employee>
    {
      public EmployeeList() { }
    }
    [XmlType(TypeName="employee")]
    public class Employee
    {
      [XmlAttribute()]
      public int id { get; set; }
      [XmlAttribute()]
      public string name { get; set; }
      [XmlAttribute()]
      public int seniorid { get; set; }
      [XmlAttribute()]
      public int budget { get; set; }
    }

    private void button3_Click(object sender, EventArgs e)
    {
      XmlSerializer ser = new XmlSerializer(typeof(EmployeeList));
      EmployeeList lst;
      using (FileStream fs = new FileStream(@"C:\employees.xml", FileMode.Open))
      {
        lst = ser.Deserialize(fs) as EmployeeList;
        System.Diagnostics.Debug.Assert(lst != null);
      }
      lst.ForEach(emp => Console.WriteLine("Employee '{0}' is budgeted {1:F0}", emp.name, emp.budget));
    }

hi apegram and sknake thanks very much for your help. i tried something like this but still not getting anywhere.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Serialization;



namespace employeebudget
{
    class Program
    {
        static void Main(string[] args)
        {

            var query = from employee in XElement.Load("employees.xml").Elements("employee")
                        select new
                        {
                            Id = (int)employee.Attribute("id"),
                            Name = (string)employee.Attribute("name"),
                            Budget = (decimal)employee.Attribute("budget"),
                            SeniorId = (int?)employee.Attribute("seniorid")
                        };
        }

        public int Getbudget()
        {
            int total = from xyz in XElement.Load("employees.xml").Elements("employee")
                         select                 
                    
         
            Name.Budget.Count + xyz.Getbudget()).Sum();
            return total;
                }
                    {
                Console.WriteLine(total);
            }

            //Pause the application
            Console.ReadLine(); 

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.