LINQ to XML - save multiple results to same class

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am a newbie to LINQ and am trying to create a function that will traverse my XML and store data to a user-define class.
My xml has a structure like:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; record<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; E01<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; E01_01<span style="color:Blue; >somedata<span style="color:Blue; </<span style="color:#A31515; E01_01<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; E01<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; E02<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; E02_16<span style="color:Blue; >somedata<span style="color:Blue; </<span style="color:#A31515; E02_16<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; E02_17<span style="color:Blue; >somedata<span style="color:Blue; </<span style="color:#A31515; E02_17<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; E02<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; record<span style="color:Blue; >

[/code]
<div style="color:Black;background-color:White; <pre>
 
[/code]
What i want to do is ensure that each of fields (E01_01, E02_16, E02_17) are assigned to the properties of my class but because each group is in its own node with a different name, I am having to do my select then use a foreach after to extract from my result
and assign the value(s) to my class properties.
I know that there must be a simpler way of doing this but I cannot figure it out. This is how I am doing it now.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
XDocument doc = XDocument.Load(<span style="color:#A31515; @"C:XMLDatatripdata.xml");
<span style="color:Green; // Get all the full "record" records from the XML
IEnumerable<XElement> records =
<span style="color:Blue; from rec <span style="color:Blue; in doc.Descendants(<span style="color:#A31515; "Header").Descendants(<span style="color:#A31515; "Record")
<span style="color:Blue; select rec;

<span style="color:Green; // Process each "Record"
<span style="color:Blue; foreach (XElement rec <span style="color:Blue; in records)
{
<span style="color:Green; // create a new trip sheet object
TripSheet trip = <span style="color:Blue; new TripSheet() ;

<span style="color:Green; // Get the PCR number
<span style="color:Blue; var E01 = <span style="color:Blue; from item <span style="color:Blue; in rec.Descendants(<span style="color:#A31515; "E01")
<span style="color:Blue; select <span style="color:Blue; new {E0101 = item.Element(<span style="color:#A31515; "E01_01").Value} ;

<span style="color:Green; // Save the PCR number
<span style="color:Blue; foreach (<span style="color:Blue; var item <span style="color:Blue; in E01) {trip.PatientCareNumber = item.E0101;}

<span style="color:Green; // Get the Mileage record
<span style="color:Blue; var E02 = <span style="color:Blue; from item <span style="color:Blue; in rec.Descendants(<span style="color:#A31515; "E02")
<span style="color:Blue; select <span style="color:Blue; new
{
E0216 = item.Element(<span style="color:#A31515; "E02_16").Value,
E0217 = item.Element(<span style="color:#A31515; "E02_17").Value


};
<span style="color:Green; // Save the Mileage
<span style="color:Blue; foreach (<span style="color:Blue; var item <span style="color:Blue; in E02)
{
trip.MilesOut = item.E0216;
trip.MilesScene = item.E0217;


}


[/code]
<br/>


View the full article
 
Back
Top