The idea I have is simple: I have this XML:
<Balance>
<LocationName>Location 1</LocationName>
<Longitude>39.74</Longitude>
<Latitude>-104.99</Latitude>
<RTI>
<Container>12</Container>
<Pallet>54</Pallet>
</RTI>
</Balance>
<Balance>
<LocationName>Location 2</LocationName>
<Longitude>39.73</Longitude>
<Latitude>-104.8</Latitude>
<RTI>
<Container>21</Container>
<Pallet>45</Pallet>
</RTI>
</Balance>
In an ASP.NET project, I have a controller function that takes a String called id as a parameter (for example, "location 1"). I need to find a node in this XML file of which the location child element has the same value as the given id, and copy its data to the object calling it. What would be the simplest way to do this? I have some code here to visualize what I mean:
The controller:
public PartialViewResult BalanceBarChart(String id)
{
Balance balanceParam = new Balance(id);
return PartialView(balanceParam);
}
And the class:
public class Balance
{
public String LocationName { get; set; }
public int pallets { get; set; }
public int containers { get; set; }
public double Longitude { get; set; }
public double Latitude { get; set; }
public Balance(String id)
{
loadXMLData(id);
}
private void loadXMLData(String id)
{
XElement doc = XElement.Load("~/App_Data/saldotracking.xml");
var queryResult =
from result in doc.Elements("Balance")
where (String)doc.Attribute("LocationName") == id
select result;
// copy results to class attributes
}
}
And the view:
@model TodayIT.TrackOnline.Maps.Models.Balance
@model.LocationName
@model.Longitude
//etc
The Microsoft tutorials on Linq aren't very clear on what to do with the results of a Linq Query. If I did it right, the variable queryResult should hold exactly one Balance node with the elements I want. How do I copy these values to the class attributes?
No comments:
Post a Comment