Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在xml查询中将字符串转换为数字类型_C#_Xml_Linq_Converters - Fatal编程技术网

C# 无法在xml查询中将字符串转换为数字类型

C# 无法在xml查询中将字符串转换为数字类型,c#,xml,linq,converters,C#,Xml,Linq,Converters,我有一个从xml文件中提取数据的查询 文件看起来像这样 <?xml version="1.0" encoding="utf-8" ?> <CommodityData> <Commodity id="Corn"> <Year value="2019"> <AcresPlanted value="90005000" /> <AcresHarvested value="82017000" />

我有一个从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
我可以通过这个查询检索所有数据

 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;
    }
我还尝试将其他3个值转换为double或long,以便将它们除以1000000,但所有转换尝试都会产生异常

输入字符串格式不正确

我不能转换查询中的数据吗?从xml文件中提取后,是否必须转换它

非常感谢您的帮助。

Convert.ToDecimalstring执行特定于区域性的转换,如果区域性的小数分隔符不是“”,则可能会失败

选项1:使用Convert.ToDecimalstring、CultureInfo.InvariantCulture

选项2:XAttribute具有内置的转换功能,可以根据XML规则(例如decimal?属性)进行转换。使用我更喜欢的第二个选项,您的代码将是:

_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();

Gotter…尝试了这两种方法,每种方法都会产生相同的异常。您确定输入数据中没有问题吗?我用你的样本中两年的元素进行了测试,效果很好。Groot…好吧,这让我看了一下文件,是的,我在xml部分的末尾有两个空部分,这可能是问题所在…删除它们后,它现在可以工作了。顺便说一下,小数点是多少?这样做的方法不起作用,VS将其视为代码错误。您是将属性声明为十进制还是十进制?-在前一种情况下,如果YieldBuAcre始终存在,则将强制转换替换为decimal.Gotter…是的,将属性类型更改为decimal?用十进制?在查询中。我试过十进制和十进制?
{
    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();
_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();