C# XML Deserializing to a Class

I can't figure out how to deserialize the below XML into a class. Spent a few hours on google search examples but I am having a hard time figuring this out.

Each "entry" element  has "field" elements associated with it based on what was changed. The field element has a name attribute which indicates the name of the field and either <nv> and/or <ov> child elements to indicate NewValue or OldValue, respectively. I only care about the NewValue <nv>.

For every entry element, I want to deserialize the timetamp and effective attribues and the field values into a class. Each entry element should have its own record, so the class somehow implement IEnumerable. It would look something like this:

(0) timestamp="2011122818084801", effective="20030224", HROrganization="XYZ", PrimaryWorkAssignment="0"
(1) timestamp="2011122818113424", effective="20101101", PrimaryWorkAssignment="1"
(2) ...
(3) ...

<log>
   <entry timestamp="2011122818084801" actor="1156" 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.GivenName" t="30" s="50" d="0">
         <nv><![CDATA[John]]></nv>
      </field>
      <field name="Name.FamilyName" t="30" s="50" d="0">
         <nv><![CDATA[Doe]]></nv>
      </field>
      <field name="HomeCountry" t="0" s="3" d="0">
         <nv><![CDATA[US]]></nv>
      </field>
      <field name="PrimaryWorkAssignment" t="1" s="4" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="StartDate" t="5">
         <nv><![CDATA[19900101]]></nv>
      </field>
   </entry>
   <entry timestamp="2011122818113424" actor="1156" authenticatedactor="SYSTEM" effective="20101101" caller="WorkAssignment.ImportRecord" action="SetPrimaryWorkAssignment" type="update" s="HCM" id="01346688-b157-0000-0000-15a54e08dd31">
      <field name="PrimaryWorkAssignment" t="1" s="4" d="0">
         <ov><![CDATA[0]]></ov>
         <nv><![CDATA[1]]></nv>
      </field>
   </entry>
   <entry timestamp="2012022918363888" actor="2056" authenticatedactor="2056" effective="20120102" caller="Employee.Update" action="Update" type="update" s="HCM" id="0135c24c-60a8-0000-0000-40d74e08dd31">
      <field name="AdjustedStartDate" t="5">
         <ov><![CDATA[00000000]]></ov>
         <nv><![CDATA[20030224]]></nv>
      </field>
   </entry>
   <entry applied="2013061004000798" timestamp="2013060415355983" actor="1057" authenticatedactor="1057" session="" effective="20130610" caller="WorkAssignment.UpdateFromPositionOrJob" action="BuildPosClassSearch" type="update" s="HCM" id="013eedee-6260-0000-0001-49164e08dd31">
      <field name="PrimaryWorkAssignment" t="1" s="4" d="0">
         <nv><![CDATA[1]]></nv>
      </field>
      <field name="StartDate" 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 code snippet should do what you want.

// Set the filename to the XML file on the next line.
XDocument xdoc = XDocument.Load("Path & Filename to XML file");

List<Entry> results = (from node in xdoc.Descendants("entry")
                       select new Entry()
                       {
                           Timestamp = node.Attribute("timestamp").Value,
                           Effective = node.Attribute("effective").Value,
                           FieldList = (from child in node.Descendants("field")
                                        select new Fields()
                                        {
                                            Name = child.Attribute("name").Value,
                                            NewValue = child.Element("nv").Value
                                        }
                                       ).ToList()
                      }).ToList();

results.Dump();

// Classes to be created by the Linq query
public class Entry
{
    private List<Fields> fields = new List<Fields>();
    
    public string Timestamp { get; set; }
    public string Effective { get; set; }
    public List<Fields> FieldList { get; set; }
}

public class Fields
{
    public string Name { get; set; }
    public string NewValue { get; set; }
}

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:
Thank you I was banging my head trying to figure out how to do it using System.Xml.Serialization. Your way is much cleaner and gives me more control over filtering based on the entry attributes.
0
Fernando SotoRetiredCommented:
Not a problem nightshadz, glad I was able to help.
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.