无法在 xml 查询中将字符串转换为数字类型
Can't convert string to number type in xml query
我有一个从 xml 文件中提取数据的查询。
文件看起来像这样
<?xml version="1.0" encoding="utf-8" ?>
<CommodityData>
<Commodity id="Corn">
<Year value="2019">
<AcresPlanted value="90005000" />
<AcresHarvested value="82017000" />
<AcresProduced value="13900651000" />
<YieldBuAcre value="169.5" />
</Year>
<Year value="2018">
<AcresPlanted value="89129000" />
<AcresHarvested value="81740000" />
<AcresProduced value="14420101000" />
<YieldBuAcre value="176.4" />
</Year>
//...and so on
我有数据的 DTO
public class GrainDataNass
{
public string Commodity { get; set; }
public string Year { get; set; }
public string AcresPlanted { get; set; }
public string AcresHarvested { get; set; }
public string AcresProduced { get; set; }
public string YieldBuAcre { get; set; }
}
我可以用这个查询检索所有数据
public List<GrainDataNass> GetGrainData(string commodity)
{
var query = _doc.Descendants("Commodity")
.Where(el => commodity == (string) el.Attribute("id"))
.Descendants("Year").Select(u => new GrainDataNass
{
Commodity = commodity,
Year = u.Attribute("value")?.Value.ToString(),
AcresPlanted = u.Element("AcresPlanted")?.Attribute("value")?.Value,
AcresHarvested = u.Element("AcresHarvested")?.Attribute("value")?.Value,
AcresProduced = u.Element("AcresProduced")?.Attribute("value")?.Value,
YieldBuAcre = u.Element("YieldBuAcre")?.Attribute("value")?.Value
}).ToList();
return query;
}
我使用此数据生成的图表需要数据字段 AcresPlanted、AcresHarvested 等为数字。小数,因为我将面积和生产区域除以 1,000,000,因为它们太大了。 (是的,我更改了 Dto 以反映类型更改)
我尝试在查询本身中这样做,就像这样,首先只使用收益率数据,因为它看起来已经像小数了,因为大多数值都是小数,即 156.4、160.3 等
{
Commodity = commodity,
Year = u.Attribute("value")?.Value,
AcresPlanted = u.Element("AcresPlanted")?.Attribute("value")?.Value,
AcresHarvested = u.Element("AcresHarvested")?.Attribute("value")?.Value,
AcresProduced = u.Element("AcresProduced")?.Attribute("value")?.Value,
YieldBuAcre = Convert.ToDecimal(u.Element("YieldBuAcre")?.Attribute("value")?.Value)
}).ToList();
我还尝试将其他 3 个值转换为双精度或长整数,这样我就可以将它们除以 1,000,000,但所有转换尝试都产生了异常
"Input string not in correct format"
我可以不转换查询中的数据吗?从 xml 文件中提取后是否必须转换它?
感谢任何帮助。
Convert.ToDecimal(string)
执行特定于文化的转换,如果您的文化的小数点分隔符不是“.”,该转换可能会失败。
选项 1:使用 Convert.ToDecimal(string, CultureInfo.InvariantCulture)
选项 2:XAttribute 具有内置的转换工具,作为转换运算符,根据 XML 规则进行转换,例如(decimal?)attribute
。使用第二个选项(我更喜欢),您的代码将是:
_doc.Descendants("Commodity")
.Where(el => commodity == (string) el.Attribute("id"))
.Descendants("Year").Select(u => new GrainDataNass
{
Year = u.Attribute("value")?.Value.ToString(),
AcresPlanted = u.Element("AcresPlanted")?.Attribute("value")?.Value,
AcresHarvested = u.Element("AcresHarvested")?.Attribute("value")?.Value,
AcresProduced = u.Element("AcresProduced")?.Attribute("value")?.Value,
YieldBuAcre = (decimal?)u.Element("YieldBuAcre")?.Attribute("value")
}).ToList();
我有一个从 xml 文件中提取数据的查询。
文件看起来像这样
<?xml version="1.0" encoding="utf-8" ?>
<CommodityData>
<Commodity id="Corn">
<Year value="2019">
<AcresPlanted value="90005000" />
<AcresHarvested value="82017000" />
<AcresProduced value="13900651000" />
<YieldBuAcre value="169.5" />
</Year>
<Year value="2018">
<AcresPlanted value="89129000" />
<AcresHarvested value="81740000" />
<AcresProduced value="14420101000" />
<YieldBuAcre value="176.4" />
</Year>
//...and so on
我有数据的 DTO
public class GrainDataNass
{
public string Commodity { get; set; }
public string Year { get; set; }
public string AcresPlanted { get; set; }
public string AcresHarvested { get; set; }
public string AcresProduced { get; set; }
public string YieldBuAcre { get; set; }
}
我可以用这个查询检索所有数据
public List<GrainDataNass> GetGrainData(string commodity)
{
var query = _doc.Descendants("Commodity")
.Where(el => commodity == (string) el.Attribute("id"))
.Descendants("Year").Select(u => new GrainDataNass
{
Commodity = commodity,
Year = u.Attribute("value")?.Value.ToString(),
AcresPlanted = u.Element("AcresPlanted")?.Attribute("value")?.Value,
AcresHarvested = u.Element("AcresHarvested")?.Attribute("value")?.Value,
AcresProduced = u.Element("AcresProduced")?.Attribute("value")?.Value,
YieldBuAcre = u.Element("YieldBuAcre")?.Attribute("value")?.Value
}).ToList();
return query;
}
我使用此数据生成的图表需要数据字段 AcresPlanted、AcresHarvested 等为数字。小数,因为我将面积和生产区域除以 1,000,000,因为它们太大了。 (是的,我更改了 Dto 以反映类型更改)
我尝试在查询本身中这样做,就像这样,首先只使用收益率数据,因为它看起来已经像小数了,因为大多数值都是小数,即 156.4、160.3 等
{
Commodity = commodity,
Year = u.Attribute("value")?.Value,
AcresPlanted = u.Element("AcresPlanted")?.Attribute("value")?.Value,
AcresHarvested = u.Element("AcresHarvested")?.Attribute("value")?.Value,
AcresProduced = u.Element("AcresProduced")?.Attribute("value")?.Value,
YieldBuAcre = Convert.ToDecimal(u.Element("YieldBuAcre")?.Attribute("value")?.Value)
}).ToList();
我还尝试将其他 3 个值转换为双精度或长整数,这样我就可以将它们除以 1,000,000,但所有转换尝试都产生了异常
"Input string not in correct format"
我可以不转换查询中的数据吗?从 xml 文件中提取后是否必须转换它?
感谢任何帮助。
Convert.ToDecimal(string)
执行特定于文化的转换,如果您的文化的小数点分隔符不是“.”,该转换可能会失败。
选项 1:使用 Convert.ToDecimal(string, CultureInfo.InvariantCulture)
选项 2:XAttribute 具有内置的转换工具,作为转换运算符,根据 XML 规则进行转换,例如(decimal?)attribute
。使用第二个选项(我更喜欢),您的代码将是:
_doc.Descendants("Commodity")
.Where(el => commodity == (string) el.Attribute("id"))
.Descendants("Year").Select(u => new GrainDataNass
{
Year = u.Attribute("value")?.Value.ToString(),
AcresPlanted = u.Element("AcresPlanted")?.Attribute("value")?.Value,
AcresHarvested = u.Element("AcresHarvested")?.Attribute("value")?.Value,
AcresProduced = u.Element("AcresProduced")?.Attribute("value")?.Value,
YieldBuAcre = (decimal?)u.Element("YieldBuAcre")?.Attribute("value")
}).ToList();