如何使用if语句检查XML元素并添加条件? [英] How do I use if statement to check for XML elements and add conditions?

查看:1062
本文介绍了如何使用if语句检查XML元素并添加条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有xml文档,我想使用if语句来设置条件,以便用户执行条件状态。

条件:

1.如果存在phoneInternationDirectDialID,则必须为011或者没有

2.如果存在TelephoneCountryCodeID,则必须为1或者没有

3。必须包含TelephoneAreaCodeID且必须为3位数 - 数字

4.必须包含TelephoneExchangeID且必须包含3位数字

5.必须包含TelephoneSubscriberID且包含4位数字数字。



如何编写if语句来检查和执行这些条件。在此先感谢。



我的xml文档

此xml文档位于对象(vb.net)中objXMLInputDoc

I have xml document which I want to use a if statement to put conditions on so that the user does what the condition states.
Conditions:
1. If phoneInternationDirectDialID is present, it has to be 011 or nothing
2. If TelephoneCountryCodeID is present, it has to be 1 or nothing
3. TelephoneAreaCodeID must be included and has to be 3 digits - numeric
4. TelephoneExchangeID must be included and must have 3 numeric digits
5. TelephoneSubscriberID must be included and have 4 numeric digits.

How do I write a if statement to check and enforce these conditions. Thanks in advance.

My xml document
This xml document is in an object (vb.net) objXMLInputDoc

<phoneNumber>
	<phoneInternationDirectDialID>612</phoneInternationDirectDialID><!--has to be 011 or nothing-->
	<phoneCountryCodeID>+93</phoneCountryCodeID><!--has to be 1 or nothing-->
	<phoneAreaCodeID>780</phoneAreaCodeID><!--has to be included and 3 digits-->
	<phoneExchangeID>347</phoneExchangeID><!--has to be included and 3 digits-->
	<phoneSubscriberID>8701</phoneSubscriberID><!--Has to be included and 4 digits-->
</phoneNumber>





我尝试了什么:



我想使用if这样的语句,但不知道如何完成它。



What I have tried:

I want to use if statements like this but not sure how to finish it.

'If phone number provided check for valid area code, TelephoneExchangeID and TelephoneSubscriberID
        'Check if TelephoneInternationDirectDialID and it must be 011
 If Not objXMLInputDoc.DocumentElement.SelectSingleNode("TelephoneNumber") Is Nothing Then
   'Add conditions here
 End If

推荐答案

你走在正确的轨道上! SelectSingleNode [ ^ ]是获取单个 XmlNode 的正确方法 ^ ]。



注意:你使用了错误的节点名称。没有 TelephoneNumber ,但 phoneNumber



如果你想进行几次比较,首先要检查XmlNode是否为Nothing,如果不是,请查看 [ ^ ]。



You're on the right track! SelectSingleNode[^] is proper method to get single XmlNode[^].

Note: you're using wrong name of node. There's no TelephoneNumber, but phoneNumber.

If you would like to make several comparisons, you have to firstly check if XmlNode is not Nothing and if it's not, then check out its Value[^].

Dim n As XmlNode = objXMLInputDoc.DocumentElement.SelectSingleNode("phoneInternationDirectDialID")
If n IsNot Nothing Then
    If n.Value = "011" Then
        'here your logic
    End If
End If





以同样的方式,你必须进行其余的比较......



In the same way, you have to do the rest of your comparisons...


这篇关于如何使用if语句检查XML元素并添加条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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