Inbound JSON to WebApi returning 500 error

Kmcnet 786 Reputation points
2024-08-24T18:47:05.2733333+00:00

Hello everyone and thanks for the help in advance. I am working on a Asp.Net Core WebApi that receives json from a third party external vendor. Here is a sample message returned:

{"?xml":{"@version":"1.0","@encoding":"utf-8"},"Message":{"@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","@DatatypesVersion":"20170715","@TransportVersion":"20170715","@TransactionDomain":"SCRIPT","@TransactionVersion":"20170715","@StructuresVersion":"20170715","@ECLVersion":"20170715","Header":{"To":{"@Qualifier":"D","#text":"D122190:C111392"},"From":{"@Qualifier":"M","#text":"WEX"},"MessageID":"6fe89f5815c74dd793630563380ad822","RelatesToMessageID":"12320263","SentTime":"2024-08-24T00:19:14.76364Z","Security":{"UsernameToken":{"Username":"WEX","Password":{"@Type":"PasswordDigest","#text":"XXXX"}}},"SenderSoftware":{"SenderSoftwareDeveloper":"WENO_DEV","SenderSoftwareProduct":"WEX","SenderSoftwareVersionRelease":"V1"}},"Body":{"Verify":{"VerifyStatus":{"Code":"010","Description":"NewRx sent by the WOL account method."}}}}}

I'm not really sure why they are returning the segment

{"?xml":{"@version":"1.0","@encoding":"utf-8"}

and I am not sure how to work around it given

public class Root
{
public ?xml ?xml { get; set; }
public Message Message { get; set; }
}

throws a syntax error and removing the question mark des not map the data correctly. Any help would be appreciated.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,550 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jerry Fu - MSFT 666 Reputation points Microsoft Vendor
    2024-08-26T08:23:22.8366667+00:00

    Hi @Kmcnet,

    This JSON structure represents XML metadata, you could try following steps to deserialize the json.

    Install package Microsoft.AspNetCore.Mvc.NewtonsoftJson, then specify the property name by using Newtonsoft.Json;

        public class Root
        {
            [JsonProperty("?xml")]
            public XmlDetails Xml { get; set; }
        }
        public class XmlDetails
        {
            [JsonProperty("@version")]
            public string Version { get; set; }
            [JsonProperty("@encoding")]
            public string Encoding { get; set; }
        }
    

    Set up default serializer in program.cs

    builder.Services.AddControllers().AddNewtonsoftJson();
    

    Test result:

    {"?xml":{"@version":"1.0","@encoding":"utf-8"}}
    

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Bruce (SqlWork.com) 64,826 Reputation points
    2024-08-26T17:52:57.05+00:00

    if you only need to deserialize then you ignore the special characters. the deserializer will bind by ignoring the invalid characters in the name. copy your sample json, then in VS paste special -> paste as json classes

    then its just:

    var obj = System.Text.Json.JsonSerializer.Deserialize<Rootobject>(jsonString);
    
    

    if you need to serialize, then you will need to define the json property names (I'd probably do this anyway):

         public class Rootobject
         {
             [System.Text.Json.Serialization.JsonPropertyName("?xml")]
             public Xml xml { get; set; }
             public Message Message { get; set; }
         }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.