none
How do I create new xml document from another xml document? RRS feed

  • Question

  • I am using C# and need to create a new xml document based on sample original xml document. I have 2 samples of what the new document should look like. I also have an original document that my code will read to get element values to create the new xml document.

    aobjXmlInputDoc can represent many different documents which C# have to read and then based on notification at the bottom, create a new xml document.

    objPrepHearDoc is an object which has various nodes to be created for new xml message.

    Can someone help?

    Here is a xml document with Setting Held notification at the bottom which C# should read and create a sample message. This in C# is in aobjXmlInputDoc

    <Integration>
    	<Case>
    		<CaseNumber>62-CR-16-844</CaseNumber>
    		<Hearing ID="198305249" Op="E" xmlns:reslib="urn:reslib">
    			<HearingType Word="ACT">Type Hearing</HearingType>
    			<HearingComment>Some comment</HearingComment>
    			<Setting ID="28647874" Date="08/31/2018" Op="E">
    				<HearingDate>08/31/2018</HearingDate>
    				<CourtSessionBlock InternalCourtSessionBlockID="1616333655">
    					<StartTime>1:15 PM</StartTime>
    					<EndTime>1:15 PM</EndTime>
    				</CourtSessionBlock>
    				<CourtroomMinutes Op="A">
    					<HearingResult Op="E" Word="HELD">Held</HearingResult>
    				</CourtroomMinutes>
    			</Setting>
    		</Hearing>
    	</Case>
    	<IntegrationConditions>
    		<IntegrationCondition Word="HRGTRLSET" Description="HearingTrialSetting">
    			<NotificationEvent notificationType="PrepareHearingDocument" elementState="Existing" elementName="Setting" elementKey="198305249" subElementKey="28647874">SettingHeld</NotificationEvent>
    		</IntegrationCondition>
    	</IntegrationConditions>
    </Integration>


    This in C# is in aobjXmlInputDoc

    Here is second original document with Hearing Scheduled notification at the bottom.

    <?xml version="1.0" encoding="UTF-8"?>
    <Integration>
    	<Case>
    		<CaseNumber>62-VB-19-27</CaseNumber>
    		<Hearing ID="252919460" Op="A">
    			<HearingType Op="A" Word="ACT">Hearing Appointment</HearingType>
    			<HearingComment>Some comment</HearingComment>
    			<Setting ID="32178675" InternalSettingID="1631575987" Date="01/22/2019" Op="A">
    				<HearingDate Op="A">01/22/2019</HearingDate>
    				<CourtSessionBlock InternalCourtSessionBlockID="1617068812">
    					<StartTime Op="A">9:00 AM</StartTime>
    					<EndTime Op="A">9:15 AM</EndTime>
    				</CourtSessionBlock>
    			</Setting>
    		</Hearing>
    	</Case>
    	<IntegrationConditions>
    		<IntegrationCondition Word="HRGTRLSET" Description="HearingTrialSetting">
    			<NotificationEvent notificationType="HearingTrialSettingNotification" elementState="New" elementName="Hearing" elementKey="252919460">HearingScheduled</NotificationEvent>
    		</IntegrationCondition>
    	</IntegrationConditions>
    </Integration>


    When aobjXmlInputDoc has HearingScheduled notification at the bottom, my C# should create the following sample message.

    <PrepareHearingDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="PrepareHearingDocument.xsd">
    	<CaseNumber>70-PR-18-8</CaseNumber>
    	<MessageEvent>HearingScheduled</MessageEvent>
    	<Hearing hearingKey="252918946">
    		<HearingTypeText code="ACT">Account Hearing</HearingTypeText>
    		<HearingCommentText>Some random thought</HearingCommentText>
    		<Setting settingKey="32178453">
    			<HearingDateTimes>
    				<HearingDatetimeStart>2019-01-07T08:30:00-06:00</HearingDatetimeStart>
    				<HearingDatetimeEnd>2019-01-07T08:45:00-06:00</HearingDatetimeEnd>
    			</HearingDateTimes>
    			<SettingStatus>Scheduled</SettingStatus>
    		</Setting>
    	</Hearing>
    </PrepareHearingDocument>
    

    Setting Held is another sample message that will be create when aobjXmlInputDoc has SettingHeld notification at the bottom.

    <PrepareHearingDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="PrepareHearingDocument.xsd">
    	<CaseNumber>70-PR-18-8</CaseNumber>
    	<MessageEvent settingKey="32178454">SettingHeld</MessageEvent>
    	<Hearing hearingKey="252918946">
    		<HearingTypeText code="ACT">Account Hearing</HearingTypeText>
    		<HearingCommentText>Some random thought</HearingCommentText>
    		<Setting settingKey="32178453">
    			<HearingDateTimes>
    				<HearingDatetimeStart>2019-01-07T08:30:00-06:00</HearingDatetimeStart>
    				<HearingDatetimeEnd>2019-01-07T08:45:00-06:00</HearingDatetimeEnd>
    			</HearingDateTimes>
    			<SettingStatus>Cancelled</SettingStatus>
    		</Setting>
    		<Setting settingKey="32178454">
    			<HearingDateTimes>
    				<HearingDatetimeStart>2019-01-07T08:30:00-06:00</HearingDatetimeStart>
    				<HearingDatetimeEnd>2019-01-07T08:45:00-06:00</HearingDatetimeEnd>
    			</HearingDateTimes>
    			<SettingStatus>Held</SettingStatus>
    		</Setting>
    	</Hearing>
    </PrepareHearingDocument>
    

    My C# class

    using System.Xml;
    using System.Security.Cryptography;
    namespace Msc.Integration.Mncis.Service.v4
    {
        class PrepareHearingDocument : CamperUpdates
        {
            public void ProcessPrepareHearingDocument(ref Msc.Integration.MessageBroker.Library.v4.Broker aobjBroker, ref System.Xml.XmlDocument aobjXmlInputDoc, ref Microsoft.VisualBasic.Collection aobjInstantiatedObjectsCollection, XmlNode aobjxmNotificationEventNode)
            {
                aobjBroker.PostMessageWarehouseInformationalMessage("Processing an PrepareHearingDocument message", 1);
                ConservatorService.PrepareHearingDocument objPrepHearDoc = new ConservatorService.PrepareHearingDocument();
    
    
          
                //Connect to MMC webservice
                ConservatorService.ConservatorServiceClient objConService = GetServiceClient(ref aobjBroker);
    
                //Get elementKey
                string strEventId = aobjxmNotificationEventNode.SelectSingleNode("@elementKey").InnerText;
                //Get CaseNumber
                objPrepHearDoc.CaseNumber = aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/CaseNumber").InnerText;
    }
    }
    }


    Thursday, February 7, 2019 9:40 PM

All replies

  • I am not sure if I understand the pain point but here is my 2 cents. 

    If you don't have C# classes corresponding to xml schema then you will need to get the SettingStatus using xpath or something and compare it. You can have some enum for ease and readability like

    enumSettingStatus {     SettingHeld,     HearingScheduled }
    XmlElement docElem = doc.CreateElement(string.Empty, "PrepareHearingDocument"string.Empty);

                    doc.AppendChild(docElem);
     
                    XmlElement caseNoElem = doc.CreateElement(string.Empty, "CaseNumber"string.Empty);
                    XmlText caseNoTxt = doc.CreateTextNode(caseNo);
                    caseNoElem.AppendChild(caseNoTxt);
                    docElem.AppendChild(caseNoElem); //and rest..
     and depending on the setting you can create the settingstatus node.

    if (notifSetting == SettingStatus.HearingScheduled.ToString()) {

    //Create XmlDocument with setting node for hearing scheduled manually

    }

    else if (notifSetting != SettingStatus.SettingHeld.ToString())
    {
       //Create different xml doc with setting status setting held manually.
    }

    not sure if this is wanted, but if it helps please let me know. :)


    Hari Mothukuri

    Thursday, February 7, 2019 11:08 PM
  • Hi winkimjr2,

    What is the relationship and regular between this three xml files? How do you get the new xml file from the three original xml files?

    You could try to use the XmlDocument or XDocument which we always use for XML.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 11, 2019 6:17 AM
    Moderator