我有两个元素的xml都显示日期。然而,一个显示日期与时间,而另一个没有时间。
我想比较这两个日期,但我只需要删除时间。
我该怎么做呢?
在我的vb程序中,我想检查是否stOrderDAte <= strmessageProducedDateTime
以下是我的xml元素,日期为:
<OrderEndDate>2015-08-06</OrderEndDate>
<DomesticAbuseNoContactOrderNotification messageProducedDateTime="2015-08-07T17:25:12-05:00" xmlns="http://www.courts.state.mn.us/CourtXML/3">
以下是我的vb代码:
Protected Overrides Sub ProcessMessage(ByRef aobjBroker As MessageBroker.Library.v4.Broker, _
ByRef aobjXMLInputSoapEnvelopeDoc As System.Xml.XmlDocument, ByRef aobjInstantiatedObjectsCollection As Microsoft.VisualBasic.Collection)
MyBase.ProcessMessage(aobjBroker, aobjXMLInputSoapEnvelopeDoc, aobjInstantiatedObjectsCollection)
Dim strSoapText As String
Dim strNotificationEvent As String
Dim strOrderEndDate As String
Dim objNameTable As NameTable
Dim objXMLNameSpaceManager As XmlNamespaceManager
Dim strmessageProducedDateTime As String
Dim strErrorDescription As String
'create a namespace manager used for queries into inputmessage (because of namespace)
objNameTable = New NameTable
objXMLNameSpaceManager = New XmlNamespaceManager(objNameTable)
objXMLNameSpaceManager.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope")
objXMLNameSpaceManager.AddNamespace("wsa", "http://schemas.xmlsoap.org/ws/2004/08/addressing")
objXMLNameSpaceManager.AddNamespace("msc", "http://www.courts.state.mn.us/CourtXML/3")
strSoapText = aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/soap:Fault/soap:Reason/soap:Text", objXMLNameSpaceManager).InnerText
strNotificationEvent = aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/soap:Fault/soap:Detail/soap:Envelope/soap:Body/msc:DomesticAbuseNoContactOrderNotification/msc:NotificationEvent", objXMLNameSpaceManager).InnerText
strOrderEndDate = aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/soap:Fault/soap:Detail/soap:Envelope/soap:Body/msc:DomesticAbuseNoContactOrderNotification/msc:DomesticAbuseNoContactOrder/msc:OrderEndDate", objXMLNameSpaceManager).InnerText
strmessageProducedDateTime = aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/soap:Fault/soap:Detail/soap:Envelope/soap:Body/msc:DomesticAbuseNoContactOrderNotification/@messageProducedDateTime", objXMLNameSpaceManager).InnerText
strErrorDescription = aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/soap:Fault/soap:Detail/ErrorDescription", objXMLNameSpaceManager).InnerText
If (strNotificationEvent = "OrderDeleted") And (strOrderEndDate <= strmessageProducedDateTime) Or (strErrorDescription = "Protection Order Number does not exist on an 'OrderDeleted' transaction.") Then
aobjBroker.PostMessageWarehouseInformationalMessage("Deleting an expired DANCO. No outstanding failure", 1)
ElseIf (strOrderEndDate <= strmessageProducedDateTime) And (strErrorDescription = "Protection Order Number does not exist on an ‘Order Modified’ transaction.") Then
aobjBroker.PostMessageWarehouseInformationalMessage("Skipping pass when modifying expired DANCO. No outstanding failure", 1)
Else
Throw New Exception("Rejection_From_BCA")
End If
End Sub
答案 0 :(得分:0)
伙计们在评论中是正确的,我赞扬他们。这里有代码示例,只提供字符串input1
和input2
:
Dim date1 As Date
Dim date2 As Date
If DateTime.TryParse(OrderEndDate, date1) AndAlso DateTime.TryParse(messageProducedDateTime, date2) Then
If date1.Date < date2.Date Then
'do something
Else
'do something else
End If
Else
'throw error that one of dates cannot be parsed
End If
在您的代码中
将以下两个声明替换为:
Dim orderEndDate As DateTime
Dim messageProducedDateTime As DateTime
并使用此代码:
strSoapText = aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/soap:Fault/soap:Reason/soap:Text", objXMLNameSpaceManager).InnerText
strNotificationEvent = aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/soap:Fault/soap:Detail/soap:Envelope/soap:Body/msc:DomesticAbuseNoContactOrderNotification/msc:NotificationEvent", objXMLNameSpaceManager).InnerText
orderEndDate = DateTime.Parse(aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/soap:Fault/soap:Detail/soap:Envelope/soap:Body/msc:DomesticAbuseNoContactOrderNotification/msc:DomesticAbuseNoContactOrder/msc:OrderEndDate", objXMLNameSpaceManager).InnerText)
messageProducedDateTime = DateTime.Parse(aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/soap:Fault/soap:Detail/soap:Envelope/soap:Body/msc:DomesticAbuseNoContactOrderNotification/@messageProducedDateTime", objXMLNameSpaceManager).InnerText)
strErrorDescription = aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/soap:Fault/soap:Detail/ErrorDescription", objXMLNameSpaceManager).InnerText
If (strNotificationEvent = "OrderDeleted") And bothDatesPresent And (orderEndDate <= messageProducedDateTime.Date) Or (strErrorDescription = "Protection Order Number does not exist on an 'OrderDeleted' transaction.") Then
aobjBroker.PostMessageWarehouseInformationalMessage("Deleting an expired DANCO. No outstanding failure", 1)
ElseIf (orderEndDate <= messageProducedDateTime.Date) And (strErrorDescription = "Protection Order Number does not exist on an ‘Order Modified’ transaction.") Then
aobjBroker.PostMessageWarehouseInformationalMessage("Skipping pass when modifying expired DANCO. No outstanding failure", 1)
Else
Throw New Exception("Rejection_From_BCA")
End If
如果解析日期有任何问题,请研究Date.Parse()如何解析各种日期格式。
除此之外,您的代码还存在以下小问题:
And
/ Or
运算符替换为AndAlso
/ OrElse
。soapText
而不是strSoapText
。答案 1 :(得分:0)
试试这个
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Sub Main()
Dim input As String =
"<Root>" & _
"<OrderEndDate>2015-08-06</OrderEndDate>" & _
"<DomesticAbuseNoContactOrderNotification messageProducedDateTime=""2015-08-07T17:25:12-05:00"" xmlns="""">" & _
"</DomesticAbuseNoContactOrderNotification>" & _
"</Root>"
Dim element As XElement = XElement.Parse(input)
Dim orderEndDate As DateTime = element.Descendants("OrderEndDate").Select(Function(x) (CType(x.Value, String))).FirstOrDefault()
Dim messageProducedDateTime As DateTime = element.Descendants("DomesticAbuseNoContactOrderNotification").FirstOrDefault().Attribute("messageProducedDateTime").Value
Dim _time As TimeSpan = messageProducedDateTime.TimeOfDay
End Sub
End Module