Angela
Angela

Reputation: 69

How do I check if a value nothing in it?

I would like to check the value length for <ss:Id>4566</ss:Id> if it is greater than zero. For now I don't know if this is a string or integer. I also want to check if <ss:Chapter>169</ss:Chapter> the value length is greater than zero. For now I don't know if this is a string or integer.

Xml:

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

VB code:

'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

Upvotes: 0

Views: 87

Answers (1)

vesan
vesan

Reputation: 3369

You're calling SelectSingleNode and comparing the result to Nothing. But the node does exist, so it will not be Nothing. If the node exists, you should take the node that SelectSingleNode returns and do this check:

String.IsNullOrEmpty(Node.Value)

Of course, this will only look at the value of the node as string (because Value is always a string). So it will check if there is at least one character in the node. It doesn't check if it's valid (or even if it's a number). But from your question I understood that this is what you need.

Edit: I'm not really a VB.NET person (more C#), but I would probably do it like this. Your code has a minor problem with the logic - if the first field ("StatueId/Id") is found, it will not check the second field.

    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

Btw is it supposed to be "statute" or "statue"?

Upvotes: 1

Related Questions