0

I have a xml document that I am using for each to loop through parties. I need to get PartyID and DateOfBirth. I am getting PartyID, but DateOfBirth is showing 0001-01-01T00:00:00.

XML Document

<Integration>
    <Case>
        <CaseEvent Date="06/14/2010" ID="252945068">
            <PartyID>9919636</PartyID>
        </CaseEvent>
        <CaseParty ID="9919636">
            <DateOfBirth>04/27/1910</DateOfBirth>
        </CaseParty>
    </Case>
    <IntegrationConditions>
        <IntegrationCondition Word="TAWQ" Description="Inserts">
            <NotificationEvent notificationType="TAWQ" elementKey="252945068">InsertSomething</NotificationEvent>
        </IntegrationCondition>
    </IntegrationConditions>
</Integration>

Expected result

<InsertPWBRorAOS xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
    <RelatedParties>
        <CaseParty>
            <DateOfBirth>04/27/1910</DateOfBirth>
            <PartyId>9919636</PartyId>
        </CaseParty>
    </RelatedParties>
</InsertPWBRorAOS>

Actual result

<InsertPWBRorAOS xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
    <RelatedParties>
        <CaseParty>
            <DateOfBirth>0001-01-01T00:00:00</DateOfBirth>
            <PartyId>9919636</PartyId>
        </CaseParty>
    </RelatedParties>
</InsertPWBRorAOS>

My VB.NET code

Public Shared Sub ProcessInsertPWBRorAOS(ByRef aobjXmlInputDoc As System.Xml.XmlDocument, ByVal aobjxmlNotificationEventNode As XmlNode)
    Dim objInsertPWBRorAOS As MMGService.InsertPWBRorAOS = New MMGService.InsertPWBRorAOS
    Dim objCaseParty As MMGService.CaseParty
    Dim objxmlEventPartyIDNode As XmlNode
    Dim strEventId As String

    strEventId = aobjxmlNotificationEventNode.SelectSingleNode("@elementKey").InnerText
    objCaseParty = New MMGService.CaseParty()

    'Loop through all PartyIDNodes in CaseEvent with ID equal to NotificationEvent's elementKey 
    For Each objxmlEventPartyIDNode In aobjXmlInputDoc.DocumentElement.SelectNodes("Case/CaseEvent[@ID=" + strEventId + "]/PartyID")
        strPartyID = objxmlEventPartyIDNode.InnerText
        objCaseParty.PartyId = strPartyID
        'DateofBirth
        objCaseParty.DateOfBirth = dtmDateOfBirth
        objInsertPWBRorAOS.RelatedParties(i) = objCaseParty
        i += 1
    Next
End Sub
Bart Hofland
  • 3,700
  • 1
  • 13
  • 22
  • You add an undefined variable `dtmDateOfBirth`.. Perhaps use something like `dtmDateOfBirth = aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/CaseParty[@ID=" + strPartyID + "]/DateOfBirth").InnerText` – Theo Jul 15 '19 at 19:01
  • The objCaseParty object have DateOfBirth and other elements. This is why I used the object to get the PartyID – CsharpLearner2019 Jul 15 '19 at 19:29
  • I know, but where do you set the value for `dtmDateOfBirth`? As your code is now, it is simply an undefined variable. – Theo Jul 15 '19 at 19:30
  • Oh I now get it. Thanks. I will add it as you suggested. If this work then I will let you know. – CsharpLearner2019 Jul 15 '19 at 19:32

1 Answers1

0

I just stumbled upon this unanswered question of yours. It's almost a year old. Based on the comments so far, the question still seems to be open. So I would like to provide you with an answer, hoping that it can still be useful for you.

It is not entirely clear to me what the actual issue is. There might be two issues with your date of birth value in your result.

  1. The date does not seem to be set; it is set to January 1, 0001. That is the minimum value of a Date (System.DateTime) variable and also the initial value of class member variables of that data type when they are not explicitly initialized.

  2. The formatting of the date value is different (compared to the "Expected result" section).

I will address both potential issues below.

Regarding point 1:

The field objCaseParty.DateOfBirth is set to the value of variable dtmDateOfBirth. So it seems that this dtmDateOfBirth variable is not set/initialized correctly as well. I would suggest to search your code for all references to the dtmDateOfBirth variable. Perhaps you forgot to set its value somewhere, or you want to additionally check if the date of birth is not yet set (and abort execution and warn the user when necessary).

Regarding point 2:

Normally, any fields of type Date will be stored/serialized in XML using the format yyyy-MM-ddTHH:mm:ss.

If you only want to drop the time part, you could use/modify the XML Schema Definition (XSD) and explicitly include/change the type of the field to xs:date. (Eventually, the XSD will probably contain a line similar to something like <xs:element name="DateOfBirth" type="xs:date"/>.) With this change, the date will be stored in the format yyyy-MM-dd.

If you want the date values to be stored explicitly in the format MM/dd/yyyy, I see no other way than to store/serialize it as a string value. It will require some additional programming, but there are several answers already available on StackOverflow that will get you on your way with this, like:

Note regarding your For Each loop:

Inside your For Each loop, you use an iteration variable i. This variable is being incremented within the For Each loop, but it is not declared with an initial value before the loop starts. I guess you are missing a line like Dim i As Integer = 0.

Bart Hofland
  • 3,700
  • 1
  • 13
  • 22