Newbie Linq question

ChrisMDrew
ChrisMDrew used Ask the Experts™
on
Hi - excuse the probably basic question but I have just come acroess LINQ and am wondering if it will be easier to work with than using the native C# XML classes.  I have an XDocument which I have passed to a web service and unpacked into an XDocument again.  My format is as follows:

<Synchronize>
  <Parts>
    <Part>
      <ID>37</ID>
      <Type>part</Type>
      <Number>OX-F1245</Number>
      <Description>ALTERNATOR</Description>
      <IsLifed>Yes</IsLifed>
      <ManufacturerNumber></ManufacturerNumber>
      <Created>No</Created>
      <Updated>Yes</Updated>
    </Part>
  </Parts>
</Synchronize>

I will be extending this later to have a <Components> and <History> sections later but for testing this will do.  Now for example if I want to get a list of ALL of the parts how would I do this?

If I wanted a list of all parts which had a type of say '1', how would I do that?

Thanks for any suggestions just to get me started...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
See if this helps.

using System;
using System.Linq;
using System.Xml.Linq;

namespace _27240894
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument xdoc = XDocument.Load("input.xml");

            #region All Parts

            Console.WriteLine("All Parts\n");

            var parts1 = from part in xdoc.Descendants("Part")
                        select part;

            foreach (var part in parts1)
            {
                Console.WriteLine(part.Element("Description").Value);
            }

            #endregion

            Console.WriteLine("\n\nParts By ID");

            #region Parts With Specific ID

            var parts2 = from part in xdoc.Descendants("Part")
                        where part.Element("ID").Value == "1"
                        select part;

            foreach (var part in parts2)
            {
                Console.WriteLine(part.Element("Description").Value);
            }

            #endregion


            Console.ReadKey();
        }
    }
}

Open in new window

Author

Commented:
Thanks - one question - how would this work if you have the same named item (say part) in multiple sections?  Say Parts\Part and Foobar\Part?  Does the path to the element not matter?  Not a problem in my case just interested.
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
how would this work if you have the same named item (say part) in multiple sections?
You could accomplish this in a couple of ways. Either Descendants or Elements should work. The difference between the two is:

Descendants - Children at any generation
Elements - Immediate children

So you could do something like this:
var parts1 = from part in xdoc.Descendants("Parts")
                              .Elements("Part")
             select part;

Open in new window

Author

Commented:
Thanks - worked perfectly

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial