none
How do I fix vb code not to display empty xml elements? RRS feed

  • Question

  • My vb code is displaying empty elements from XML document that I did not select in the code. The DateOfBirth and DateOfDeath elements are displayed when I run the code. I do not want them displayed. 

    These elements are inside the object that have the only element I need it's value displayed which is PartyId. The object name is objCaseParty. In my code I am selecting the PartyId from this object. However the DateOfBirth and DateOfDeath are also displayed in the result xml even though I have not selected them in my vb code.

    How do I remove these two elements from being displayed?

    Here is the xml document vb code is using to out put xml result below.

    <?xml version="1.0" encoding="UTF-8"?>
    <Integration xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:tsg="http://tsgweb.com" xmlns:IXML="http://tsgweb.com" PackageID="MMG Updates">
    	<Case>
    		<CaseEvent xmlns:reslib="urn:reslib" Op="E" Date="06/14/2019" ID="252945068" InternalEventID="1851137356">
    			<EventDate>06/14/2019</EventDate>
    			<RevDate>06/14/2021</RevDate>
    			<CompDate Op="E">07/10/2019</CompDate>
    			<EventType Word="NOPERWELL">Personal Well-being Report [R]</EventType>
    			<PartyID InternalPartyID="536367004">9919636</PartyID>
    		</CaseEvent>
    		<CaseParty ID="9919636" InternalCasePartyID="1655313331" InternalPartyID="536367004">
    			<CasePartyName Current="true" ID="10069192" InternalNameID="1615543609">
    				<NameFirst>Ynne</NameFirst>
    				<NameLast>Zeett</NameLast>
    			</CasePartyName>
    			<DateOfBirth InternalDOBID="536308471">04/27/1981</DateOfBirth>
    		</CaseParty>
    	</Case>
    	<IntegrationConditions>
    		<IntegrationCondition Word="MMGUPD" Description="MMG Updates">
    			<NotificationEvent notificationType="MMGUpdate" elementState="Add" elementName="CaseEvent" elementKey="252945068">InsertPWBRorAOS</NotificationEvent>
    		</IntegrationCondition>
    	</IntegrationConditions>
    </Integration>

    Here is expected result

    <InsertPWBRorAOS xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
    <CaseNumber>10-PR-19-125</CaseNumber>
    <CompletedDate>2019-07-10T00:00:00</CompletedDate>
    <DueDate>2021-06-14T00:00:00</DueDate>
    <EventDate>2019-06-14T00:00:00</EventDate>
    <EventType>NOPERWELL</EventType>
    <RelatedParties>
    <CaseParty>
    <PartyId>9919636</PartyId>
    </CaseParty>
    </RelatedParties>
    </InsertPWBRorAOS>

    Here is the result I am currently getting which has additional elements with no values. I do not want these elements returned

    <InsertPWBRorAOS xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
    <CaseNumber>10-PR-19-125</CaseNumber>
    <CompletedDate>2019-07-10T00:00:00</CompletedDate>
    <DueDate>2021-06-14T00:00:00</DueDate>
    <EventDate>2019-06-14T00:00:00</EventDate>
    <EventType>NOPERWELL</EventType>
    <RelatedParties>
    <CaseParty>
    <DateOfBirth xsi:nil="true"/>
    <DateOfDeath xsi:nil="true"/>
    <PartyId>9919636</PartyId>
    <PartyType xsi:nil="true"/>
    </CaseParty>
    </RelatedParties>
    </InsertPWBRorAOS>

    Here is my vb code

    Option Explicit On
    Option Strict On
    Imports System
    Imports System.Xml
    Imports System.Xml.XPath
    Imports System.Collections.Generic
    Imports System.Net.ServicePointManager
    
    Public Class InsertPWBRorAOS : Inherits MMGUpdates
    Public Shared Sub ProcessInsertPWBRorAOS(ByRef aobjBroker As Msc.Integration.MessageBroker.Library.v4.Broker, ByRef aobjXmlInputDoc As System.Xml.XmlDocument, ByVal aobjxmlNotificationEventNode As XmlNode)
            aobjBroker.PostMessageWarehouseInformationalMessage("Processing an InsertPWBRorAOS message", 1)
    
            Dim objMMGService As MMGService.GuardianServiceClient = GetServiceClient(aobjBroker)
            Dim objInsertPWBRorAOS As MMGService.InsertPWBRorAOS = New MMGService.InsertPWBRorAOS
            Dim objCaseParty As MMGService.CaseParty
            Dim intPartiesCount As Integer
            Dim i As Integer
            Dim strEventId As String
            Dim strPartyID As String
            Dim objxmlEventPartyIDNode As XmlNode
            
    
            'CaseNumber
            objInsertPWBRorAOS.CaseNumber = aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/CaseNumber").InnerText
            'EventID
            strEventId = aobjxmlNotificationEventNode.SelectSingleNode("@elementKey").InnerText
            'CaseEvent
            objxmlCaseEventNode = aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/CaseEvent[@Op='E' and @ID=" + strEventId + "]")
            'CompletedDate
            objInsertPWBRorAOS.CompletedDate = CDate(aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/CaseEvent[@ID=" + strEventId + "]/CompDate").InnerText)
            'DueDate is RevDate
            objInsertPWBRorAOS.DueDate = CDate(aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/CaseEvent[@ID=" + strEventId + "]/RevDate").InnerText)
            'EventDate
            objInsertPWBRorAOS.EventDate = CDate(aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/CaseEvent[@ID=" + strEventId + "]/EventDate").InnerText)
            'EventType 
            strEventType = aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/CaseEvent[@ID=" + strEventId + "]/EventType/@Word").InnerText
            objInsertPWBRorAOS.EventType = CType([Enum].Parse(GetType(MMGService.EventTypes), strEventType), MMGService.EventTypes)
            'Count parties in CaseEvent
    
    	intPartiesCount = aobjXmlInputDoc.DocumentElement.SelectNodes("Case/CaseEvent[@ID=" + strEventId + "]/PartyID").Count
            'RelatedParties
            objInsertPWBRorAOS.RelatedParties = New MMGService.CaseParty(intPartiesCount - 1) {}
            'i = 0
    
            '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 = New MMGService.CaseParty()
                objCaseParty.PartyId = strPartyID 'I only want to display PartyID
                objInsertPWBRorAOS.RelatedParties(i) = objCaseParty
                i += 1
            Next
    End Sub


    • Edited by winkimjr2 Thursday, July 11, 2019 1:32 PM Added Imports to my vb.net code
    Thursday, July 11, 2019 1:23 PM

All replies

  • Some guessing here, cause you've omitted the important parts. But imho the DOB is part of

    objCaseParty = New MMGService.CaseParty()
    in your loop. Seems that it is there marked as mandatory element. Thus it is added during serialization as empty/nil.
    Thursday, July 11, 2019 1:28 PM
  • I do have imports on my code. I forgot to add them on code here. I am adding them now. 
    Thursday, July 11, 2019 1:31 PM
  • Is there a way to force it not to be displayed even if it is marked as mandatory element?
    Thursday, July 11, 2019 3:30 PM
  • Is there a way? Sure.

    But should you go it? Maybe. You need to understand, that the serialization just does what is defined to be correct. Thus, when there should be no DOB in the result, when empty/nil, then the first step is to look at the definition. Normally this is the current XML schema. You need the change it and you need to tell all participating parties (pun intended) that the schema has changed.

    Otherwise you can simply look into the definition of MMGService.CaseParty and change the serialization attributes.

    Thursday, July 11, 2019 3:39 PM
  • I looked in the reference.vb and found the following. These are optional elements 

    <System.Runtime.Serialization.OptionalFieldAttribute()>  _Private DateOfBirthField As System.Nullable(Of Date) 
    
    <System.Runtime.Serialization.OptionalFieldAttribute()>  _Private DateOfDeathField As System.Nullable(Of Date)

    • Edited by winkimjr2 Thursday, July 11, 2019 3:53 PM Added vb code for serialization
    Thursday, July 11, 2019 3:47 PM