Reputation: 23
SOLVED - Thanks for your help everyone!
Before I get to my code, I'd like to offer a short explanation of what it is I'm trying to accomplish. I have an XML file in the Data folder, which has 3 Elements per descendant: Month, High and Low. Each descendant is already in the appropriate order based on Month, but I need to convert the high and low strings to doubles, so that I can use them to plot points on a graph. That is the part I am unable to accomplish.
What I have so far:
double month = 25;
const double temp = 275;
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Data\\";
XDocument xmlSource;
private void btnShowAlbany_Click(object sender, RoutedEventArgs e)
{
try
{
//Brush, Polyline, PointCollection for drawing
SolidColorBrush myBrush = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
Polyline myLine = new Polyline();
myLine.Stroke = myBrush;
myLine.StrokeThickness = 2;
PointCollection myPoints = new PointCollection();
double theTemp = 0;
//Get XML data
xmlSource = XDocument.Load(path + @"\Albany.xml");
var myTemps = from tmp in xmlSource.Descendants("temp")
select new
{
Month = tmp.Element("Month").Value,
High = tmp.Element("High").Value,
Low = tmp.Element("Low").Value
};
//Pull out data for points
foreach (var x in myTemps)
{
double high = Convert.ToDouble(x.High);
double low = Convert.ToDouble(x.Low);
theTemp = (high + low) / 2;
myPoints.Add(new Point(month, temp - theTemp));
month += 25;
}
//Add points to line, add line as child of canvas
myLine.Points = myPoints;
albanyCanvas.Children.Add(myLine);
}
catch
{
MessageBox.Show("Error accessing data source", "Show Temps", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
Any guidance is appreciated, thanks.
(Note: My canvas grid values begin at 25 X, 275 Y, eg. a temp of 25 in Feb would be plotted @ 50,250)
The XML file:
<?xml version="1.0" encoding="utf-8"?>
<Temps>
<Temp>
<Month>Jan</Month>
<High>31</High>
<Low>15</Low>
</Temp>
<Temp>
<Month>Feb</Month>
<High>35</High>
<Low>17</Low>
</Temp>
<Temp>
<Month>Mar</Month>
<High>44</High>
<Low>26</Low>
</Temp>
<Temp>
<Month>Apr</Month>
<High>58</High>
<Low>37</Low>
</Temp>
<Temp>
<Month>May</Month>
<High>69</High>
<Low>47</Low>
</Temp>
<Temp>
<Month>Jun</Month>
<High>78</High>
<Low>57</Low>
</Temp>
<Temp>
<Month>Jul</Month>
<High>82</High>
<Low>62</Low>
</Temp>
<Temp>
<Month>Aug</Month>
<High>80</High>
<Low>60</Low>
</Temp>
<Temp>
<Month>Sep</Month>
<High>72</High>
<Low>52</Low>
</Temp>
<Temp>
<Month>Oct</Month>
<High>60</High>
<Low>40</Low>
</Temp>
<Temp>
<Month>Nov</Month>
<High>48</High>
<Low>32</Low>
</Temp>
<Temp>
<Month>Dec</Month>
<High>36</High>
<Low>21</Low>
</Temp>
</Temps>
Upvotes: 0
Views: 225
Reputation: 65451
Is from tmp in xmlSource.Descendants("temp")
correct?
Your XML nodes are called Temp
, and XML is case sensitive
Your double high = Convert.ToDouble(x.High);
looks okay so long at the data in the XML is valid, for safety you might try something like
double high;
if (!double.TryParse(x.High, out high)) {
// handle error
}
Also, as others have pointed out, try to break your code up in smaller methods, it will be more readable and more maintainable that way
Upvotes: 4
Reputation: 3909
2 ways.
You can do Convert.ToDbouble(string) as suggested above but an exception would be thrown if it can't parse out the number.
You can try
double d;
Double.TryParse(strToParse, out d);
Upvotes: 0
Reputation: 10221
XML is case sensitive. Fix your selector to be .Descendants("Temp")
Upvotes: 3