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>
2
  • Please give a sample input and desired output. Commented Feb 25, 2017 at 3:47
  • Possible duplicates check this link1 and link2. Commented Feb 25, 2017 at 3:49

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the input - that's really helpful and gives me the idea to do what I need. I'm trying to get the <code> 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 <code> value 5002.
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?

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.