.NET Core WebApi Json and XML Request

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1148 Views - Last Post: 26 September 2021 - 11:57 AM Rate Topic: -----

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 362
  • Joined: 25-May 16

.NET Core WebApi Json and XML Request

Posted 24 September 2021 - 09:05 AM

Hello everyone, my company is going through changes of services. One of our services takes an XML object and converts it to JSON and sends that JSON to our WebApi as a POST request.

The new service we are moving to does not have functionality to do transformation so we will now have to send the payload as an XML.

So in the Startup.cs class I added this
services.AddControllers().AddJsonOptions().AddXmlDataContractSerializerFormatters();



That now allows our API to accept both XML or JSON. The issue now comes from the formatting

Our Controller is like this
[HttpPost]
public async Task<IActionResult> Update([FromBody] ObjectRoot someObject)
{
 // code
}


In the json, the root (ObjectRoot) would be ignored and would look like something like this
{
 "Information" : {
    "MoreInfo": .....
  }
}


However, in the XML, it is expecting something like
<ObjectRoot>
 <Information>
  <MoreInfo>
  </MoreInfo>
 </Information>
</ObjectRoot>



The issue is, the XML we are receiving does not have the ObjectRoot part and starts with the Information part as the root. I tried using the XmlIgnore attribute but it is not ignoring it for the api but only when serializing it after the fact.

Any ideas on how I can have this work out?

Is This A Good Question/Topic? 0
  • +

Replies To: .NET Core WebApi Json and XML Request

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7863
  • View blog
  • Posts: 26,270
  • Joined: 05-May 12

Re: .NET Core WebApi Json and XML Request

Posted 24 September 2021 - 12:05 PM

What happens if you go all out and implement IXmlSerializable?
Was This Post Helpful? 0
  • +
  • -

#3 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 362
  • Joined: 25-May 16

Re: .NET Core WebApi Json and XML Request

Posted 24 September 2021 - 12:21 PM

Not sure how I would be able to implement it. Would it have to be at the controller level?

When I implement the interface, I have 3 methods that I have to implement and define
1. GetSchema
2. ReadXml
3. WriteXml

The Controller is expecting the root object to be sent with the root name being part of the schema for the xml.

With Json, the root level starts off with a {

Not sure how I can keep the parameter in the controller to be the same to accept both XML and JSON while omitting the root element for the XML part of it.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7863
  • View blog
  • Posts: 26,270
  • Joined: 05-May 12

Re: .NET Core WebApi Json and XML Request

Posted 24 September 2021 - 12:30 PM

Ah good point.
Was This Post Helpful? 0
  • +
  • -

#5 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 362
  • Joined: 25-May 16

Re: .NET Core WebApi Json and XML Request

Posted 24 September 2021 - 12:38 PM

Maybe there is not an easy way for me to do this.

I can force the controller to accept a json that can start off like
{
 "ObjectRoot": {
    "Information" : {
      "MoreInfo": ......
   }
  }
}



So maybe I will have to change the parameter and take the lower level object and define it in the JSON to include the root, which will be the "Information" property in that case (if I go one level down) and I won't have any issues to worry about since the XML already starts off with that.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7863
  • View blog
  • Posts: 26,270
  • Joined: 05-May 12

Re: .NET Core WebApi Json and XML Request

Posted 24 September 2021 - 12:42 PM

I feel like the key might be found here, Parameter Binding in ASP.NET Web API, but I can't focus enough right now to try to narrow it down.
Was This Post Helpful? 0
  • +
  • -

#7 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 256
  • View blog
  • Posts: 1,369
  • Joined: 05-December 13

Re: .NET Core WebApi Json and XML Request

Posted 24 September 2021 - 07:46 PM

Try this :
            XmlDocument doc1 = new();
            doc1.LoadXml("<ObjectRoot><Information><MoreInfo></MoreInfo></Information></ObjectRoot>");
            string jsonText = JsonConvert.SerializeXmlNode(doc1, Formatting.Indented, true);


            XmlDocument doc3 = JsonConvert.DeserializeXmlNode(File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "json.json")));



The JsonConvert.SerializeXmlNode has a parameter you can set to true, which is the omitRootObject parameter. Setting it to true, will not parse the root, essentially skipping it. Doc3 can be used to do the reverse and read json to XML.
Omitted : https://prnt.sc/1tj5ugc
Parameter : http://prntscr.com/1tj64ty

While this is what you want, I think its worth mentioning that sometimes XDocument is easier to work with than working with Xml.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7863
  • View blog
  • Posts: 26,270
  • Joined: 05-May 12

Re: .NET Core WebApi Json and XML Request

Posted 25 September 2021 - 07:14 AM

Yes, that is the behavior he is looking for, but how does he tell ASP.NET WebAPI to behave that way when receiving an incoming request where the request content is automatically converted into an object?
Was This Post Helpful? 0
  • +
  • -

#9 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 256
  • View blog
  • Posts: 1,369
  • Joined: 05-December 13

Re: .NET Core WebApi Json and XML Request

Posted 25 September 2021 - 03:28 PM

You don't. And you can't. I simply addressed the question, not the problem.

The issue here is an architectural problem.

If the project managers of the new API won't allow them to change the controllers parameters or change how the API is called, then they (as a team) need to go back to the drawing board together.

This post has been edited by Sheepings: 25 September 2021 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

#10 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 256
  • View blog
  • Posts: 1,369
  • Joined: 05-December 13

Re: .NET Core WebApi Json and XML Request

Posted 25 September 2021 - 03:41 PM

I should also point out that removing a root element or skipping it, or converting nodes to json after doing so, technically isn't parsing valid XML, as valid XML should have a clearly defined root.

As we speak, I wouldn't like to be the dev who picks up this project to continue working on afterwards.
Was This Post Helpful? 1
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7863
  • View blog
  • Posts: 26,270
  • Joined: 05-May 12

Re: .NET Core WebApi Json and XML Request

Posted 25 September 2021 - 03:47 PM

This may help: Routing by content type
Was This Post Helpful? 0
  • +
  • -

#12 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 256
  • View blog
  • Posts: 1,369
  • Joined: 05-December 13

Re: .NET Core WebApi Json and XML Request

Posted 25 September 2021 - 04:06 PM

Hmm, but won't that only work if they are allowed to edit the API?

It's my understanding that they are against changing the API call, so your post brings them full circle as does p1/#7.

OP needs to discuss with their project manager.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7863
  • View blog
  • Posts: 26,270
  • Joined: 05-May 12

Re: .NET Core WebApi Json and XML Request

Posted 25 September 2021 - 04:45 PM

The API's URL route will look the same. The only difference will be that if the POST has a content type of JSON, then he'll use his code from post #1, and then if the content type is XML, then he'll need to provide another method that accepts the XML of the correct shape. So from the caller's point of view, the API has not changed because they are still sending the same XML to the same URL. It's just being processed differently.
Was This Post Helpful? 0
  • +
  • -

#14 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 256
  • View blog
  • Posts: 1,369
  • Joined: 05-December 13

Re: .NET Core WebApi Json and XML Request

Posted 25 September 2021 - 06:51 PM

Well that is only assuming that they are allowed to edit the API/Controller etc. And if that may be the case, then they really would be better off using what I provided above on p1/#7 instead of dealing with all that code linked on that page you provided on p1/#11.

They would only need to send in the XML, and then break it down upon being received. After all, didn't the OP say they could send it in XML or Json?

View Postfearfulsc2, on 24 September 2021 - 07:21 PM, said:

Not sure how I can keep the parameter in the controller to be the same to accept both XML and JSON while omitting the root element for the XML part of it.


Why not just send it as XML?
Maybe I am getting to old for this forum stuff, or I'm becoming senile with old age, but what was the purpose of all this json stuff again if you can send it in XML anyway?
Was This Post Helpful? 1
  • +
  • -

#15 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 256
  • View blog
  • Posts: 1,369
  • Joined: 05-December 13

Re: .NET Core WebApi Json and XML Request

Posted 25 September 2021 - 07:18 PM

Seems this topic became overly convoluted for no reason...

If your new service does not allow conversions, it would seem simple to send the payload as Xml, and then do whatever json conversions in the WebApi upon receiving it. No? I am assuming their is an obvious reason why the WebApi is using json and not XML...

To note :

Quote

The issue is, the XML we are receiving does not have the ObjectRoot part and starts with the Information part as the root.

You've created an architectural issue, because that is now no longer valid XML. And if you converted that json code back to XML, and ran it through a custom IComparer<T>, comparing it to the original XML, you'd soon find a new set of problems should you need to revert what you are doing.

If a job is worth doing, its worth doing well.

This post has been edited by Sheepings: 25 September 2021 - 07:20 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2