Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Quick Linq to XML question

Quick Linq to XML question

Scheduled Pinned Locked Moved C#
questioncsharplinqxmlhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xkrja
    wrote on last edited by
    #1

    I'm starting out with linq to xml and have a quick question: I have the following xml-file: <countries> <country code="AF" iso="4">Afghanistan</country> <country code="AL" iso="8">Albania</country> <country code="DZ" iso="12">Algeria</country> <country code="AS" iso="16">American Samoa</country> <country code="AD" iso="20">Andorra</country> . . . How can I get a result containing all the country names by using a linq expression? I tried the following but it only gives me "Afghanistan": XDocument xmlDoc = XDocument.Load("countries.xml"); var countries = from country in xmlDoc.Descendants("countries") select new { Name = country.Element("country").Value, }; Thanks for help!

    C S 2 Replies Last reply
    0
    • X xkrja

      I'm starting out with linq to xml and have a quick question: I have the following xml-file: <countries> <country code="AF" iso="4">Afghanistan</country> <country code="AL" iso="8">Albania</country> <country code="DZ" iso="12">Algeria</country> <country code="AS" iso="16">American Samoa</country> <country code="AD" iso="20">Andorra</country> . . . How can I get a result containing all the country names by using a linq expression? I tried the following but it only gives me "Afghanistan": XDocument xmlDoc = XDocument.Load("countries.xml"); var countries = from country in xmlDoc.Descendants("countries") select new { Name = country.Element("country").Value, }; Thanks for help!

      C Offline
      C Offline
      Chris Trelawny Ross
      wrote on last edited by
      #2

      Your Linq query is asking for the wrong thing from the XDocument. The Descendents() method returns descendents of the object (in this case, the XDocument) which have the specified name (in this case 'countries'). So it is yielding a single enumerable element (because your XML Document has only one element). What you really need is to get the 'country' descendents of the XDocument: from xmlDoc.Descendents("country") and then just get the Value property of the elements of the enumeration. Here's some code that works for you:

      var countries = from country in xmlDoc.Descendants("country")
          select new
          {
              Name = country.Value
          };
      
      1 Reply Last reply
      0
      • X xkrja

        I'm starting out with linq to xml and have a quick question: I have the following xml-file: <countries> <country code="AF" iso="4">Afghanistan</country> <country code="AL" iso="8">Albania</country> <country code="DZ" iso="12">Algeria</country> <country code="AS" iso="16">American Samoa</country> <country code="AD" iso="20">Andorra</country> . . . How can I get a result containing all the country names by using a linq expression? I tried the following but it only gives me "Afghanistan": XDocument xmlDoc = XDocument.Load("countries.xml"); var countries = from country in xmlDoc.Descendants("countries") select new { Name = country.Element("country").Value, }; Thanks for help!

        S Offline
        S Offline
        souidi abderrahman
        wrote on last edited by
        #3

        Can you get a list of country: :) XDocument xmlDoc = XDocument.Load("countries.xml"); List countries = from country in xmlDoc.Descendants("country") select new { Name = country.Value, };

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups