如何引发htmlelement获取属性的异常 [英] How to throw an exception for htmlelement get attribut

查看:61
本文介绍了如何引发htmlelement获取属性的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码.

i have the following code.

HtmlElement troopmov = wb.Document.All["movements"];
                troopmov.GetAttribute("mov");
                troopsmov.Text = troopmov.InnerText;


然而,运动和运动"总是在那儿发生,它们可以离开并卷土重来.所以获取属性找不到它?我如何为此例外.我必须使用try/catch吗?


how ever, movements and "mov" arnt always there, they can leave and comeback. so get attribute cant find it? how do i make an exception for this. do i have to use try/catch? and how?

推荐答案

GetAttribute 如果不存在则返回一个空字符串,因此在这种情况下您无需担心异常处理.您需要做的就是使用string.IsNullOrEmpty检查返回值,并根据需要采取适当的措施.
GetAttribute returns an empty string if it isn''t present, so you don''t need to worry about exception handling in this case. All you need to do is check the return value using string.IsNullOrEmpty and take the appropriate action as you see fit.


这篇关于如何引发htmlelement获取属性的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆