1

Can someone please help me. I'm new to XML and xElement. How can I get number of men where year = 2013? The result should be 300.

I have this XML:

<company>
  <department>
    <departmentname>Dep 1</departmentname>
    <year id = "2012">
      <men>200</men>
      <women>1000</women>
    </year>
    <year id = "2013">
      <men>300</men>
      <women>400</women>
    </year>
  </department>
</company>

I have this code (not working):

XElement company = XElement.Load(Server.MapPath(myXML.xml));
var men = (from a in company.Elements("department").Elements("year")
           where (string)a.Attribute("id").Value == "2013"
           select (string)(a.Element("men"))).ToList<string>();
1
  • what about other departments! Commented Nov 8, 2013 at 17:33

4 Answers 4

1

What about using XPath?

var xmldoc = XDocument.Parse(@"<?xml version='1.0' encoding='utf-8'?>
    <company>
      <department>
        <departmentname>Dep 1</departmentname>
        <year id = '2012'>
          <men>200</men>
          <women>1000</women>
        </year>
        <year id = '2013'>
          <men>300</men>
          <women>400</women>
        </year>
      </department>
    </company>");

Console.WriteLine(
    xmldoc.XPathSelectElement(
            "/company/department/year[@id='2013']/men").Value);
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you want it from all departments

int mens=company.Descendants("year")
                .Where(x=>x.Attribute("id").Value=="2013")
                .Sum(x=>int.Parse(x.Element("men").Value));

Your query should be

int men = (from a in company.Elements("department").Elements("year")
           where a.Attribute("id").Value == "2013"
           select int.Parse(a.Element("men").Value)).Sum();

3 Comments

I would prefer to use xElement the way I have started. How can I do this?
@user2939293 Works on my pc... your xml is not in proper format..Attributes values must be within ""
Yes, the values are within "" in the original file. I have changed it. Thank you
0
XElement company = XElement.Load(Server.MapPath(myXML.xml));

var txt = company.Descendants("men")
    .First(x => int.Parse(x.Parent.Attribute("id").Value) == 2013).Value;

Not very robust but will do the job in your case and you should get the idea.

And with LINQ syntax rather than extension method:

var txt =
    (from c in company.Descendants("men")
     where c.Parent.Attribute("id").Value == "2013"
     select c).First().Value;

2 Comments

I would prefer to use xElement the way I have started. How can I do this?
The way you started? You mean using LINQ syntax rather than extension methods?
0

Ok guys. I used this and it's working Thank you for your help!

 XElement com = (from p in company.Elements("department")
                                    where (string)p.Element("departmentname").Value == "Dep 1"
                                    select p).First();



    var men= (from a in com.Elements("year")
                              where (string)a.Attribute("id") == "2013"
                              select (string)(a.Element("men"))).ToList<string>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.