如何修复if语句来检查3个元素? [英] How do I fix my if statement to check 3 elements?

查看:74
本文介绍了如何修复if语句来检查3个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的if语句没有工作,因为我希望它工作。 objXMLInputDoc中的xml文档有3个可选元素, AddPersonContactInformation PersonTelephoneNumber PersonEmailAddress

我想做什么是检查3个元素中是否存在并继续。如果all都不存在,则if语句将退出并显示错误。

当所有元素都不存在时它正在工作。但是,当存在一个元素时,if语句仍然存在。如何修复if语句,以便在存在一个元素时,它不会以错误退出?



此示例xml文档分配给objXMLInputDoc。这就是if语句访问它及其元素的方式。

示例xml文档

 < ;   xml     version   =  1.0   编码  =  UTF-8  >  
< soap:Envelope >
< < span class =code-leadattribute> soap:正文 >
< AddPersonContactInformation >
< PersonAddress >
< AddressUSStandard >
< AddressAttention > Mr Smith < / AddressAttention >
< / AddressUSStandard >
< / PersonAddress >
<! - < P. ersonTelephoneNumber telephoneNumberUsageText =Home>
<电话> 698-478-4612< /电话>
< / PersonTelephoneNumber>
- >

< ;! - < PersonEmailAddress> Testing@gmail.com< / PersonEmailAddress> - >
< / AddPersonContactInformation >
< / soap:Body >
< / soap:Envelope >





如果声明不起作用

 如果((objXMLInputDoc.DocumentElement.SelectSingleNode(   AddPersonContactInforma / PersonAddress  没什么并且(objXMLInputDoc.DocumentElement.SelectSingleNode(  AddPersonContactInformation / PersonTelephoneNumber   Nothing (objXMLInputDoc.DocumentElement.SelectSingleNode(< span class =code-string>  AddPersonContactInformation / PersonEmailAddress   Nothing ))然后 
' < span class =code-comment>退出
退出 Sub
结束 如果





我是什么尝试过:



 如果((objXMLInputDoc。 DocumentElement.SelectSingleNode(  AddPersonContactInformation / PersonAddress   Nothing (objXMLInputDoc.DocumentElement.SelectSingleNode(  AddPersonContactInformation / PersonTelephoneNumber  没有(objXMLInputDoc.DocumentElement.SelectSingleNode(  AddPersonContactInformation / PersonEmailAddress   Nothing ))然后 
' 退出
退出 Sub
结束 如果

解决方案

SelectSingleNode 需要一个从当前节点开始的XPath表达式。



由于 AddPersonContactInformation 不是root元素的直接子元素,你的XPath表达式将不匹配任何内容。



将表达式更改为以<$开头c $ c> // 匹配文档中任何位置的元素:

 如果((objXMLInputDoc.DocumentElement.SelectSingleNode(  // AddPersonContactInformation / PersonAddress   Nothing  AndAlso (objXMLInputDoc.DocumentEleme nt.SelectSingleNode(  // AddPersonContactInformation / PersonTelephoneNumber  Nothing  AndAlso (objXMLInputDoc.DocumentElement.SelectSingleNode(  // AddPersonContactInformation / PersonEmailAddress   Nothing ))然后 
' 退出
退出 Sub
结束 如果



XPath教程 [ ^ ]

<小时ef =https://msdn.microsoft.com/en-us/library/ms256471(v=vs.110).aspx> XPath语法 [ ^ ]



NB:你的XML似乎无效 - 你错过了 soap 名称空间声明。)


I have the if statement below that is not working as I expect it to work. The xml document in objXMLInputDoc has 3 optional elements, AddPersonContactInformation, PersonTelephoneNumber and PersonEmailAddress
What I am trying to do is to check if any of the 3 elements exist and continue. If all are not present, the if statement will exit with an error.
It is working when all elements are not present. However when one element is present, the if statement still exists like. How do I fix the if statement so that, when there is one element present, it does not exit with an error?

This sample xml document is assigned to objXMLInputDoc. This is how if statement is accessing it and its elements.
Sample xml document

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope>
	<soap:Body>
		<AddPersonContactInformation>
			<PersonAddress>
				<AddressUSStandard>
					<AddressAttention>Mr Smith</AddressAttention>
				</AddressUSStandard>
			</PersonAddress>
			<!--<PersonTelephoneNumber telephoneNumberUsageText="Home">
				<Telephone>698-478-4612</Telephone>
			</PersonTelephoneNumber>-->
			<!--<PersonEmailAddress>Testing@gmail.com</PersonEmailAddress>-->
		</AddPersonContactInformation>
	</soap:Body>
</soap:Envelope>



If statement not working

If ((objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonAddress") Is Nothing) And (objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonTelephoneNumber") Is Nothing) And (objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonEmailAddress") Is Nothing)) Then
'exit 
 Exit Sub
End If



What I have tried:

If ((objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonAddress") Is Nothing) And (objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonTelephoneNumber") Is Nothing) And (objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonEmailAddress") Is Nothing)) Then
'exit 
 Exit Sub
End If

解决方案

SelectSingleNode expects an XPath expression which starts at the current node.

Since AddPersonContactInformation is not a direct child of the root element, your XPath expression won't match anything.

Change your expressions to start with "//" to match the element anywhere in the document:

If ((objXMLInputDoc.DocumentElement.SelectSingleNode("//AddPersonContactInformation/PersonAddress") Is Nothing) AndAlso (objXMLInputDoc.DocumentElement.SelectSingleNode("//AddPersonContactInformation/PersonTelephoneNumber") Is Nothing) AndAlso (objXMLInputDoc.DocumentElement.SelectSingleNode("//AddPersonContactInformation/PersonEmailAddress") Is Nothing)) Then
'exit 
 Exit Sub
End If


XPath Tutorial[^]
XPath Syntax[^]

(NB: Your XML seems to be invalid - you're missing the soap namespace declaration.)


这篇关于如何修复if语句来检查3个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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