c# - C# XML Linq,读取 XML 返回 NullReferenceException
问题描述
首先,我很抱歉 - 我已经搜索和搜索,尝试了一个又一个例子,一个又一个答案,在尝试了很多不同的例子之后,我仍然无法让它为自己工作。
我有一个像这样的 XML smoeth:
<?xml version="1.0" encoding="utf-8"?>
<Backup LastRun="Saturday, May 16, 2020 7:58:53 PM">
<MyVehicleElements>
<InVehicle>true</InVehicle>
... etc ...
</MyVehicleElements>
</Backup>
而且我的代码无法返回空异常,因为我一生都无法弄清楚如何阅读额外的级别。
XElement xelement = XElement.Load("PathTo\\File.xml");
IEnumerable<XElement> Backup = xelement.Elements();
foreach (var MyVehicleElements in Backup)
{
Game.DisplayNotification(MyVehicleElements.Element("InVehicle").Value);
// Should return text "true"
}
在我的头爆炸之前,请有人帮助我。先感谢您。
解决方案
谢谢楼上所有帮助过的人。遇到此问题的其他任何人的工作代码是:
XElement xelement = XElement.Load("PathTo\\File.xml");
IEnumerable<XElement> MyVehicleElements = xelement.Descendants("MyVehicleElements");
foreach (var GetElements in MyVehicleElements)
{
Game.DisplayNotification((string)GetElements.Element("InVehicle"));
}
推荐阅读
- c# - 具有可为空对象属性的 Linq“where”条件导致“调用非静态方法需要一个目标”
- c# - Unity DateTime 在 JsonUtility.FromJsonOverwrite 之后为空
- r - 将数据从 R 汇总对象提取到表中
- groovy - 为什么使用 MOP 的 Groovy metaClass .static 更改未按预期运行
- pine-script - Pinescript,如何设置和指标值仅在进入新仓位而不在指标变化时修改?
- artifactory - Artifactory 到 Artifactory 远程仓库
- svg - 用本地 SVG 替换标题中的 Font Awesome
- mysql - 错误 #1064 MySQL ''accesslogh' 附近的语法有问题
- php - 选择选项后如何获取 CHtml::dropDownList 值
- arm - 编程外部 QSPI 闪存 Raspberry Pi Pico