未使用 Linq to XML 正确解析 XML 数据

发布于 2024-12-20 05:48:41 字数 3716 浏览 0 评论 0原文

已解决 - 感谢大家的帮助!

在开始编写代码之前,我想简要解释一下我想要实现的目标。我的 Data 文件夹中有一个 XML 文件,其中每个后代有 3 个元素:月份、最高价和最低价。每个后代都已经根据月份采用适当的顺序,但我需要将高字符串和低字符串转换为双精度,以便我可以使用它们在图表上绘制点。这是我无法完成的部分。

到目前为止我所得到的:

    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);
        }
    }

感谢任何指导,谢谢。

(注意:我的画布网格值从 25 X、275 Y 开始,例如,二月份的温度为 25,将在 50,250 处绘制)

XML 文件:

<?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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眸中客 2024-12-27 05:48:41

来自 xmlSource.Descendants("temp") 中的 tmp 是否正确?

您的 XML 节点称为 Temp,并且 XML 区分大小写

您的 double high = Convert.ToDouble(x.High); 看起来没问题,只要查看 XML 中的数据即可有效,为了安全起见,您可以尝试类似的方法

double high;
if (!double.TryParse(x.High, out high)) {
    // handle error
}

此外,正如其他人指出的那样,尝试以更小的方法分解您的代码,这样它将更具可读性和更可维护性

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

此刻的回忆 2024-12-27 05:48:41

XML 区分大小写。将您的选择器修复为 .Descendants("Temp")

XML is case sensitive. Fix your selector to be .Descendants("Temp")

仅一夜美梦 2024-12-27 05:48:41

你有没有尝试过,

Convert.ToDouble(string)

Have you tried,

Convert.ToDouble(string)
笑脸一如从前 2024-12-27 05:48:41

2种方式。

您可以按照上面的建议执行 Convert.ToDbouble(string) ,但如果无法解析出数字,则会抛出异常。

你可以尝试

double d;
Double.TryParse(strToParse, out d);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文