使用命名空间解析 Xml 文件并按降序对值进行排序
我正在尝试解析以下 xml 文件: http://reports.ieso.ca/public /DispUnconsHOEP/PUB_DispUnconsHOEP_20110714.xml
我的目标是获得最大价格
及其关联的小时
和最低价格
及其关联的小时
。
我应用的逻辑如下:
解析 Xml 文件并将其传递给包含小时
和价格
对的
IList 根据基于降序对 IList 进行排序price
检索 IList 中的第一个值作为最大值,而检索 IList 中的最后一个值作为最小值。
我的代码是:
XNamespace nsPriceHr = "http://www.theIMO.com/schema";
XDocument xDocument =
XDocument.Load("http://reports.ieso.ca/public/DispUnconsHOEP/PUB_DispUnconsHOEP_20110714.xml");
XElement xEl1 = xDocument.Element(nsPriceHr + "IMODocument");
XElement xEl2 = xEl1.Element(nsPriceHr + "IMODocBody");
XElement xEl3 = xEl2.Element(nsPriceHr + "HOEPs");
var data = (from x in xEl3.Descendants(nsPriceHr + "HOEP")
orderby x.Element(nsPriceHr + "Price").Value descending
select new HourPrice
{
Hour = x.Element(nsPriceHr + "Hour").Value,
Price = x.Element(nsPriceHr + "Price").Value
})
.ToList();
我的问题是:列表没有按预期排序。 HourPrice 对象采用 Hour
和 Price
两个值,这两个数据成员都是字符串。
我正在使用 C#、.NET 3.5 SP1 并在 winforms 上工作。任何帮助表示赞赏
I am trying to parse the following xml file : http://reports.ieso.ca/public/DispUnconsHOEP/PUB_DispUnconsHOEP_20110714.xml
My goal is to get the maximum price
with its associated hour
and the minimum price
with its associated hour
.
The logic that I am applying is as follows:
Parse the Xml file and pass it to a IList which contains pairs of hour
and price
Sort the IList in Descending order based on the price
Retrieve the first value in the IList as the maximum value whereas the last value in the IList as the minimum value.
My Code is:
XNamespace nsPriceHr = "http://www.theIMO.com/schema";
XDocument xDocument =
XDocument.Load("http://reports.ieso.ca/public/DispUnconsHOEP/PUB_DispUnconsHOEP_20110714.xml");
XElement xEl1 = xDocument.Element(nsPriceHr + "IMODocument");
XElement xEl2 = xEl1.Element(nsPriceHr + "IMODocBody");
XElement xEl3 = xEl2.Element(nsPriceHr + "HOEPs");
var data = (from x in xEl3.Descendants(nsPriceHr + "HOEP")
orderby x.Element(nsPriceHr + "Price").Value descending
select new HourPrice
{
Hour = x.Element(nsPriceHr + "Hour").Value,
Price = x.Element(nsPriceHr + "Price").Value
})
.ToList();
My Problem is: the List is not getting sorted as expected. The HourPrice object which takes the two values of Hour
and Price
has both data members as string.
I am using C#, .NET 3.5 SP1 and working on winforms. Any help appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
System.Convert
。特别是,您可能对ToInt32
和ToDecimal
。这些函数接受字符串(或其他各种数据类型之一),并将其分别转换为int
或decimal
。编辑:
这是检查缺失值的一种方法:
如果您的问题是字符串不为空,但格式也不正确,则必须在转换之前将它们转换为正确的格式。即,您必须从
$6.47
等字符串中去除$
。Try using
System.Convert
. In particular, you may be interested inToInt32
andToDecimal
. These functions take a string (or one of various other data types), and convert it to anint
or adecimal
respectively.Edit:
Here is one way you might check for missing values:
If your problem is that the strings aren't empty, but aren't in the right format either, you will have to convert them to the right format before you convert. I.e., you must strip the
$
from a string like$6.47
.