如何检查其中的值是否为空?

时间:2015-03-04 20:35:59

标签: xml vb.net

我想检查<ss:Id>4566</ss:Id>的值长度是否大于零。现在我不知道这是一个字符串还是整数。 我还想检查<ss:Chapter>169</ss:Chapter>值的长度是否大于零。现在我不知道这是一个字符串还是整数。

XML:

<ss:GetStatutesRequest>
    <ss:Statute>
        <ss:StatueId>
            <ns:Id>67890</ss:Id>
        </ss:StatueId>
            <ss:Chapter>169</ss:Chapter>
    </ss:Statute>
</ss:GetStatutesRequest>

VB代码:

'Check to see if the length for Id in the ss:StatuteId node is greater than zero  
If Not objXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:StatueId/ss:Id", objXMLNameSpaceManager) Is Nothing Then

 aobjBroker.PostMessageWarehouseInformationalMessage("StatuteId found.", 1)

 ElseIf Not objXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Chapter", objXMLNameSpaceManager) Is Nothing Then
 aobjBroker.PostMessageWarehouseInformationalMessage("Chapter found.", 1)
 Else
                aobjBroker.Reply(aobjBroker.CreateSoapFault(Msc.Integration.Utility.Library.v4.Soap.udtSoapCodes.Sender, "StatuteId or Chapter is required.", Msc.Integration.Utility.Library.v4.Soap.udtSoapRoles.RoleUltimateReceiver, aobjXMLInputSoapEnvelopeDoc, "soap:InvalidMessage", "soap:Body", Msc.Integration.Utility.Library.v4.Soap.GetReplyEndpointReference(aobjXMLInputSoapEnvelopeDoc), aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("soap:Header/wsa:MessageID", objXMLNameSpaceManager).InnerText, aobjConsumer))
    Exit Sub
End If

1 个答案:

答案 0 :(得分:1)

您正在调用SelectSingleNode并将结果与​​Nothing进行比较。但节点确实存在,因此它不会是Nothing。如果节点存在,则应采用SelectSingleNode返回的节点并执行此检查:

String.IsNullOrEmpty(Node.Value)

当然,这只会将节点的值视为字符串(因为Value始终是一个字符串)。因此它将检查节点中是否至少有一个字符。它不会检查它是否有效(或者即使它是一个数字)。但是根据你的问题,我明白这就是你所需要的。

编辑: 我不是一个真正的VB.NET人员(更多C#),但我可能会这样做。您的代码在逻辑上有一个小问题 - 如果找到第一个字段(&#34; StatueId / Id&#34;),它将不会检查第二个字段。

    Dim IdNode = objXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:StatueId/ss:Id", objXMLNameSpaceManager)
    Dim ChapterNode = objXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:StatueId/ss:Id", objXMLNameSpaceManager)

    Dim BothFound = True

    If Not (IdNode Is Nothing Or String.IsNullOrEmpty(IdNode.Value)) Then
        aobjBroker.PostMessageWarehouseInformationalMessage("StatuteId found.", 1)
    Else
        BothFound = False
    End If

    If Not (ChapterNode Is Nothing Or String.IsNullOrEmpty(ChapterNode.Value)) Then
        aobjBroker.PostMessageWarehouseInformationalMessage("Chapter found.", 1)
    Else
        BothFound = False
    End If

    If BothFound Then
        'continue processing
    Else
        'error
    End If
是的,它应该是&#34;法规&#34;或&#34;雕像&#34;?