0

I have two conditions for my VB.NET code. There could be many more than one InterimConditionEvent each with or without ExpirationDate child element in XML document. I need to find ExpirationDate for the last InterimConditionEvent in the XML document. If the last InterimConditionEvent does not have a ExpirationDate then find the previous InterimConditionEvent with ExpirationDate.

how do I find the preceding InterimConditionEvent before the last one?

My xml

<Integration xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:tsg="http://tsgweb.com" xmlns:IXML="http://tsgweb.com" xmlns:CMCodeQueryHelper="urn:CMCodeQueryHelper">
<Case InternalID="1617095448" ID="12131576" xmlns:user="http://tylertechnologies.com">
    <InterimConditionEvent ID="160850209">
        <OrderDate>08/14/2015</OrderDate>
        <ExpirationDate>08/14/2015</ExpirationDate>
        <Deleted>true</Deleted>
        <InterimCondition>
            <ConditionType Word="DOMNC">Domestic No Contact</ConditionType>
            <EffectiveDate>8/14/2015</EffectiveDate>
            <EndDate>8/14/2015</EndDate>
        </InterimCondition>
    </InterimConditionEvent>
    <InterimConditionEvent ID="160850210">
        <OrderDate>08/14/2015</OrderDate>
        <Deleted>true</Deleted>
        <InterimCondition>
            <ConditionType Word="DOMNC">Domestic No Contact</ConditionType>
            <EffectiveDate>8/14/2015</EffectiveDate>
            <EndDate>8/14/2000</EndDate>
        </InterimCondition>
    </InterimConditionEvent>
</Case>

VB.NET Code

If Not objXmlCaseDoc.DocumentElement.SelectSingleNode("Case/InterimConditionEvent[(Deleted='true') and (InterimCondition/ConditionType/@Word='DOMNC')][position()=last()]/ExpirationDate") Is Nothing Then
                        strOrderEndDate = objXmlCaseDoc.DocumentElement.SelectSingleNode("Case/InterimConditionEvent[(Deleted='true') and (InterimCondition/ConditionType/@Word='DOMNC')][position()=last()]/ExpirationDate").InnerText
                    Else
                        strOrderEndDate = objXmlCaseDoc.DocumentElement.SelectSingleNode("Case/InterimConditionEvent[position()=last()]/ExpirationDate").InnerText
                    End If

How do I change the following Else statement to include and has an ExpirationDate element

strOrderEndDate = objXmlCaseDoc.DocumentElement.SelectSingleNode("Case/InterimConditionEvent/[position()=last()/ExpirationDate").InnerText
  • Possible duplicate of [find next-to-last node with xpath](http://stackoverflow.com/questions/10420638/find-next-to-last-node-with-xpath) – Andrew Morton Oct 02 '15 at 16:56
  • No clue what you meant. Was that a statement or a question? –  Oct 02 '15 at 17:06
  • I see what you meant. I will try the code they have for `last()-1` and see if it will work –  Oct 02 '15 at 17:08
  • Possible duplicate of [Getting Specific Data from XML](http://stackoverflow.com/questions/21491197/getting-specific-data-from-xml) – Paul Sweatte Oct 02 '15 at 18:50

1 Answers1

0

Use XML Linq

Imports System.Xml
Imports System.Xml.Linq
Module Module1

    Sub Main()
        Dim input As String = _
            "<Integration xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" xmlns:tsg=""http://tsgweb.com"" xmlns:IXML=""http://tsgweb.com"" xmlns:CMCodeQueryHelper=""urn:CMCodeQueryHelper"">" & _
               "<Case InternalID=""1617095448"" ID=""12131576"" xmlns:user=""http://tylertechnologies.com"">" & _
                   "<InterimConditionEvent ID=""160850209"">" & _
                       "<OrderDate>08/14/2015</OrderDate>" & _
                       "<ExpirationDate>08/14/2015</ExpirationDate>" & _
                       "<Deleted>true</Deleted>" & _
                       "<InterimCondition>" & _
                           "<ConditionType Word=""DOMNC"">Domestic No Contact</ConditionType>" & _
                           "<EffectiveDate>8/14/2015</EffectiveDate>" & _
                           "<EndDate>8/14/2015</EndDate>" & _
                       "</InterimCondition>" & _
                   "</InterimConditionEvent>" & _
                   "<InterimConditionEvent ID=""160850210"">" & _
                       "<OrderDate>08/14/2015</OrderDate>" & _
                       "<Deleted>true</Deleted>" & _
                       "<InterimCondition>" & _
                           "<ConditionType Word=""DOMNC"">Domestic No Contact</ConditionType>" & _
                           "<EffectiveDate>8/14/2015</EffectiveDate>" & _
                           "<EndDate>8/14/2000</EndDate>" & _
                       "</InterimCondition>" & _
                   "</InterimConditionEvent>" & _
               "</Case>" & _
             "</Integration>"

        Dim integration As XElement = XElement.Parse(input)
        Dim expirationDate As XElement = integration.Descendants("InterimConditionEvent").Where(Function(x) Not x.Element("ExpirationDate") Is Nothing).Last()
    End Sub

End Module
​
jdweng
  • 33,250
  • 2
  • 15
  • 20