linq to xml,处理空标签
如果源 XML 包含数字或缺少标签,则以下链接语句可以正常工作。我遇到的问题是当标签为空或使用非数字值时。可以修改此声明来处理这些情况吗?
Convert.ToInt32((string)Data.Elements("groupBy").Elements("depth").FirstOrDefault() ?? "0")
The following link statement works fine if the source XML contains a number or if the tags are missing. The problem I have is when the tags are empty or if a non-numeric value is used. Can this statement be modified to handle these situations ?
Convert.ToInt32((string)Data.Elements("groupBy").Elements("depth").FirstOrDefault() ?? "0")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知道用 LINQ 解决这个问题的方法,但如果你不能保证 XML 文档的内容,那么使用 int.TryParse() 会更容易吗?,例如
Don't know of a way to solve this with LINQ but if you cannot guarantee the content of the XML document then would it be easier to just use int.TryParse()?, e.g.
我会简单地做:
I would simply do: