[英]C# Correctly parsing an XElement's Attributes into a list (exception help)
大家好,我希望你们都快乐健康:)
我的问题如下:
首先,通过以下方式引入我的主要可枚举对象:
IEnumerable航点= file.Descendants(“ Waypoints”)。First()。Elements();
然后,我开始如下遍历它们:
foreach (XElement el in waypoints)
{
try
{
//ok so this should be one element here..
Waypoint tempWP =
new Waypoint((el.Attribute("X") != null ? Convert.ToSingle(el.Attribute("X").Value) : 0.0F),
(el.Attribute("Y") != null ? Convert.ToSingle(el.Attribute("Y").Value) : 0.0F),
(el.Attribute("Z") != null ? Convert.ToSingle(el.Attribute("Z").Value) : 0.0F),
(el.Attribute("Zone") != null ? Convert.ToUInt64(el.Attribute("Zone").Value) : 0),
(el.Attribute("Type") != null ? el.Attribute("Type").Value : ""),
(el.Attribute("SafeRadius") != null ? Convert.ToUInt32("SafeRadius") : 0),
(el.Attribute("Guid") != null ? Convert.ToUInt64("Guid") : 0));
ret.Add(tempWP);
}
catch (Exception)
{
}
}
通过调试,我知道el = Waypoint X =“ 1032.91858” Y =“-3671.47949” Z =“ 35.0046425” Zone =“ 111” Type =“ none” SafeRadius =“ 1” Guid =“ 111” /
所以XElement很好。 如您所见,我试图将X / Y / Z转换为单打,将Zone转换为UInt64 ..等,然后希望将它们添加到返回的航点列表中。
但是,每次我的foreach运行时,都会遇到一个异常……“输入字符串的格式不正确。”。 有谁知道为什么会发生这种情况? 我相信我的null检查和转换应该是正确的,不是吗?
非常感谢您的帮助。 :)
您尝试转换为数字类型的值之一是空字符串或包含字母。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.