Linq to XML Help

I have the below XML that has many entry elements containing field elements. Using C#, how do I list all field elements with a "t" attribute? Output would look something like this:

name=HROrganization,t=0,s=4,d=0
name=Employee,t=1,s=13,d=0
name=AnniversaryDate,t=5
etc...

<?xml version="1.0" encoding="UTF-8"?>
<log>
   <entry timestamp="2011122818084801" actor="1234" authenticatedactor="SYSTEM" effective="20030224" caller="EmployeeImport.CreateAllEmployees" action="ImportRecord" type="create" s="HCM" id="01346688-b157-0000-0000-0b404e08dd31">
      <field name="HROrganization" t="0" s="4" d="0">
         <nv><![CDATA[XYZ]]></nv>
      </field>
      <field name="Employee" t="1" s="13" d="0">
         <nv><![CDATA[12345678]]></nv>
      </field>
      <field name="Name.FormOfAddress" t="30" s="50" d="0">
         <nv />
      </field>
      <field name="Name.GivenName" t="30" s="50" d="0">
         <nv><![CDATA[Bill]]></nv>
      </field>
      <field name="Name.PreferredGivenName" t="30" s="50" d="0">
         <nv><![CDATA[Bill]]></nv>
      </field>
      <field name="Name.MiddleName" t="30" s="50" d="0">
         <nv />
      </field>
      <field name="AnniversaryDate" t="5">
         <nv><![CDATA[20030224]]></nv>
      </field>
</entry>
</log>

Open in new window

nightshadzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi nightshadz;

The following Linq to XML query will do what you are looking for.

XDocument xdoc = XDocument.Load("File path and name goes here");

List<string> results = (from node in xdoc.Descendants()
                        where node.Attributes("t").Count( ) > 0
                        select (node.Attributes().Aggregate("", (output, input) => output + ", " + input )).Trim(new char[] {','})).ToList();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nightshadzAuthor Commented:
Thanks!
0
Fernando SotoRetiredCommented:
Not a problem nightshadz, glad I was able to help.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

nightshadzAuthor Commented:
One more question if you don't mind - or i can create a new problem for you to answer to get points ;)

Is it possible to do this and return an XElement object? The idea is to loop over each entry element, grabbing all of the fields contained within it based on the timestamp of the entry.

This is kind of where I am at and it seems there has to be a more elegant way to loop over each record in the list.

List<string> entries = (from entry in doc.Descendants("entry")
                                    select (entry.Attributes().Aggregate("", (output, input) => output + ", " + input)).Trim(new char[] { ',' })).ToList();

            foreach (string line in entries)
            { 
                
                IEnumerable<XElement> fields =
                    (from entry in doc.Root.Descendants("entry")
                    where entry.Attribute("timestamp").Value == // ?? I want to easily pull out the timestamp value but feel i have to so some crazy string parsing. Is there a more elegant way via LINQ?
                    //select entry).First().Descendants();
            }

Open in new window

0
Fernando SotoRetiredCommented:
Hi nightshadz;

This will give you the time stamp on the entry node.

var timestamp = doc.Root.Element("entry").Attribute("timestamp").Value;

Open in new window

0
nightshadzAuthor Commented:
Thanks! While not the answer I was looking for it put me on the right track. This is what my code looks like now.

            IEnumerable<XElement> entries =
                                            (from entry in doc.Root.Elements("entry")
                                             select entry);

            foreach (XElement entry in entries)
            {
                IEnumerable<XElement> fields = doc.Root.Descendants("entry")
                    .Where(x => x.Attribute("timestamp") == entry.Attribute("timestamp"))
                    .First()
                    .Descendants();
            }

Open in new window

0
Fernando SotoRetiredCommented:
Seeming that the XML has only one entry node with a timestamp I posted the short solution. But if there is going to be more then one entry node your way will work.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.