我有以下代码:
<div class="content-inner">
<span id="item-0" class="drag-item ui-draggable ui-resizable drag-item-selected ui-draggable-disabled ui-state-disabled" id="item-0" style="left: 212px; top: 225px; width: 84px; height: 79px; z-index: 1; transform: rotate(0rad); border: 1px dashed rgb(68, 68, 68);"
aria-disabled="true">
<svg width="84" height="79" viewBox="-0.3791112600718641 0 65.8780487804878 88.875" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="none"><g id="0.8609587374124218">
<text fill="#000000" stroke="none" stroke-width="0" stroke-linecap="round" stroke-linejoin="round" x="37" y="26.28750228881836" text-anchor="middle" font-size="27px" font-family="Twine" data-textcurve="0" data-itemzoom="1 1" itemzoom="0.7842624854819976 1.125">
<tspan dy="0" x="50%">Hello</tspan>
<tspan dy="27" x="50%">Hello</tspan>
<tspan dy="27" x="50%">Hello</tspan></text></g>
</svg>
</span>
<span id="item-1" class="drag-item ui-draggable ui-resizable drag- item-selected ui-draggable-disabled ui-state-disabled" id="item-0" style="left: 212px; top: 225px; width: 84px; height: 79px; z-index: 1; transform: rotate(0rad); border: 1px dashed rgb(68, 68, 68);"
aria- disabled="true">
<text fill="#000000" stroke="none" stroke-width="0" stroke-linecap="round" stroke-linejoin="round" x="37" y="26.28750228881836" text-anchor="middle" font-size="27px" font-family="Twine" data-textcurve="0" data-itemzoom="1 1" itemzoom="0.7842624854819976 1.125">
<tspan dy="27" x="50%">Hello</tspan></text></g>
</span>
即使我认为无法找到案例,也有可能找不到节点,函数SelectSingleNode()返回null。 但是如果其中一个给出了错误,那么所有3个字符串都将是空的。我可以像下面的代码一样解决它,但我不喜欢像这样连续调用3个try / catch,有没有更好的方法呢?
string name1;
string name2;
string name3;
try
{
name1 = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name1']").GetAttributeValue("value", "");
name2 = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name2']").GetAttributeValue("value", "");
name3 = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name3']").GetAttributeValue("value", "");
}
catch(Exception)
{
name1 = "";
name2 = "";
name3 = "";
}
答案 0 :(得分:0)
检查以下解决方案可能会对您有所帮助。
try {
if (null != htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name1']") && null != htmlDoc.DocumentNode
.SelectSingleNode("//input[@name='name1']").GetAttributeValue("value", "")) {
name1 = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name1']").GetAttributeValue("value", "");
} else {
name1 = null;
}
if (htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name2']") && null != htmlDoc.DocumentNode
.SelectSingleNode("//input[@name='name2']").GetAttributeValue("value", "")) {
name2 = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name2']").GetAttributeValue("value", "");
} else {
name2 = null;
}
if (htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name3']") && null != htmlDoc.DocumentNode
.SelectSingleNode("//input[@name='name3']").GetAttributeValue("value", "")) {
name3 = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='name3']").GetAttributeValue("value", "");
} else {
name3 = null;
}
} catch (Exception e) {
e.printStackTrace();
}