0

I want to search for the text value of <code> of Accountants under a job category Accounting.

Below is the idea I am thinking to get this value, but it doesn't work. What is the correct way to access the value of <code>?

XDocument xml = XDocument.Parse(xmlString);
var accountants = from c in xml.Root.Elements("JobCategory") 
                  where c.Elements("Name").Equals("Accounting") 
                  select c;
var code = from c in accountants.Descendants("Code")
           select c;

I am using asp.net MVC. The xmlString is like this:

<JobCategories xmlns="http://api.trademe.co.nz/v1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

  <JobCategory>
  <Name>Accounting</Name>
  <Code>5001</Code>
  <SubCategories>
     <JobCategory>
       <Name>Accountants</Name>
       <Code>5002</Code>
     </JobCategory>
     <JobCategory>
       <Name>Accounts administrators</Name>
       <Code>5007</Code>
     </JobCategory>
  </SubCategories>
  </JobCategory>

  <JobCategory>
  <Name>Agriculture, fishing &amp; forestry</Name>
  <Code>5015</Code>
  <SubCategories>
     <JobCategory>
       <Name>Farming</Name>
       <Code>5016</Code>
     </JobCategory>
     <JobCategory>
       <Name>Fishing</Name>
       <Code>5017</Code>
     </JobCategory>
  </SubCategories>
  </JobCategory>

</JobCategories>
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
mmcc88
  • 177
  • 1
  • 10
  • Please give a sample input and desired output. – Sateesh Pagolu Feb 25 '17 at 03:47
  • Possible duplicates check this [link1](http://stackoverflow.com/questions/7939168/how-to-get-the-value-of-a-specific-nested-xml-element-using-linq-to-xml) and [link2](http://stackoverflow.com/questions/11821410/how-to-retrieve-element-values-of-nested-elements-in-xml-with-c-sharp). – Asif Raza Feb 25 '17 at 03:49

1 Answers1

0

You've got a number of issues

First your xml content is in a namespace but you don't use any of them in your code. If you want to access elements by name, you have to reference those names correctly.

The expression c.Elements("Name").Equals("Accounting") is nonsense. You're attempting to get the child elements called Name and comparing if that collection is equal to the string "Accounting". That clearly makes no sense at all.

Assuming you're just getting the Code values of the elements, you'd do this:

XNamespace ns = "http://api.trademe.co.nz/v1";
var codes =
    from jc in doc.Root.Elements(ns + "JobCategory")
    select (string)jc.Element(ns + "Code");
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • Thanks for the input - that's really helpful and gives me the idea to do what I need. I'm trying to get the `` value of a particular job type under a particular job category depends on the user input. So if the user select "Accounting > Accounts administrators", it should return its `` value 5002. – mmcc88 Feb 25 '17 at 10:45
  • And I do something like: `var list = from jc in xml.Root.Elements(ns + "JobCategory") where jc.Element(ns + "Name").Value == "Accounting" select jc.Element(ns + "SubCategories");` , then... `var code = from j in list.Elements(ns + "JobCategory") where j.Element(ns + "Name").Value == "Accounts administrators" select (string)j.Element(ns + "Code");` However, is there anyway to shorten the code to do the same thing? – mmcc88 Feb 25 '17 at 10:46