如何在VB.Net中的最后一个xml元素前面找到一个元素

时间:2015-10-02 16:24:54

标签: xml vb.net

我的VB.NET代码有两个条件。 XML文档中可能有多个InterimConditionEvent,每个都有ExpirationDate子元素。 我需要为XML文档中的最后一个ExpirationDate找到InterimConditionEvent。 如果上一个InterimConditionEvent没有ExpirationDate,则会在InterimConditionEvent找到之前的ExpirationDate

如何在最后一个之前找到前面的InterimConditionEvent

我的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代码

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

如何更改以下Else语句以包含并具有ExpirationDate元素

strOrderEndDate = objXmlCaseDoc.DocumentElement.SelectSingleNode("Case/InterimConditionEvent/[position()=last()/ExpirationDate").InnerText

1 个答案:

答案 0 :(得分:0)

使用XML Linq

&#13;
&#13;
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
​
&#13;
&#13;
&#13;