Saving XML Data from Endpoint to my database

Nickson 1 Reputation point
2022-10-27T14:44:41.713+00:00

I am trying to pass from a "POST" endpoint that contains data in XML format to my model class then save the same to my database.

It is possible for me to save the data when the XML is in the format below:

                            <NCBAPaymentNotificationRequest>  
                                            <User>USER</User>  
                                            <Password>pass</Password>  
                            <HashVal>NzIwNGE3MzI5YmU5MTg3ZTUwZTQ1YmRjMjA0NDc2MjUyODQ2MmQ1ODIwZTIyYzNkNDk0NTBjNjUwZTgwYmM2Yw==</HashVal>  
                                            <TransType>999</TransType>  
                                            <TransID>FTC221024YAWQ</TransID>  
                                            <TransTime>2210241702</TransTime>  
                                            <TransAmount>1.00</TransAmount>  
                                            <AccountNr>6592460859</AccountNr>  
                                            <Narrative>Test</Narrative>  
                                            <PhoneNr>66666</PhoneNr>  
                                            <CustomerName>Customer 1</CustomerName>  
                                            <Status>SUCCESS</Status>  
                            </NCBAPaymentNotificationRequest>  

However, I want it saved when the XML is in the format below. It is at this point that I am facing issues:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">  
            <soapenv:Header/>  
            <soapenv:Body>  
                            <NCBAPaymentNotificationRequest>  
                                            <User>USER</User>  
                                            <Password>pass</Password>  
                            <HashVal>NzIwNGE3MzI5YmU5MTg3ZTUwZTQ1YmRjMjA0NDc2MjUyODQ2MmQ1ODIwZTIyYzNkNDk0NTBjNjUwZTgwYmM2Yw==</HashVal>  
                                            <TransType>999</TransType>  
                                            <TransID>FTC221024YAWQ</TransID>  
                                            <TransTime>2210241702</TransTime>  
                                            <TransAmount>1.00</TransAmount>  
                                            <AccountNr>6592460859</AccountNr>  
                                            <Narrative>Test</Narrative>  
                                            <PhoneNr>66666</PhoneNr>  
                                            <CustomerName>Customer 1</CustomerName>  
                                            <Status>SUCCESS</Status>  
                            </NCBAPaymentNotificationRequest>  
            </soapenv:Body>  
</soapenv:Envelope>  

Below is my Controller Code:

        [Consumes("application/xml")]  
        [Produces("application/xml")]  
        [HttpPost]  
        public async Task<NCBAPaymentNotificationRequest> Post([FromBody] NCBAPaymentNotificationRequest value)  
        {  
            value.User = value.User;  
            value.Password = value.Password;  
            value.HashVal = value.HashVal;  
            value.TransType = value.TransType;  
            value.TransID = value.TransID;  
            value.TransTime = value.TransTime;  
            value.TransAmount = value.TransAmount;  
            value.AccountNr = value.AccountNr;  
            value.Narrative = value.Narrative;  
            value.PhoneNr = value.PhoneNr;  
            value.CustomerName = value.CustomerName;  
            value.Status = value.Status;  
  
            int createdId = mpesaRepository.Add(value);  
            if (createdId > 0)  
            {  
                value.ID = createdId;  
            }  
            return value;  
        }  
  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
0 comments No comments
{count} votes