从 xml 错误 { "Value cannot be null.rnParameter name: element" } 创建对象



我需要创建一个对象列表来解析XML文档。投射到AdvertisementFullScreen时出现问题

我收到这个错误:

{"Value cannot be null.rnParameter name: element"}
我代码:

var advFs = loadedData.Element("xxx")
                           .Elements("advertisementsFullScreen")
                           .Elements("advertisement") // RESULT BELOW
                           .Select(x=>new AdvertisementFullScreen()
                           {
                               Id = (int)x.Element("id"),
                               ImageUrl = (string)x.Element("imageUrl"),
                               DisplayTime = new TimeSpan(0, 0, (int)x.Element("displayTime"))
                           }).ToList();

投影前的结果:

[0] = <advertisement id="7001" imageUrl="C:/xxx/Test-data/data-offline/Assets/advertisementsFullScreen/1.png" displayTime="5" isSelfPromo="1"></advertisement>

您选择了Element(),但是id, imageUrl, displayTime不是元素-它们是属性。

修改查询(只修改重要部分):

Id = (int)x.Attribute("id"),
ImageUrl = (string)x.Attribute("imageUrl"),
DisplayTime = new TimeSpan(0, 0, (int)x.Attribute("displayTime"))

相关内容

最新更新