0

Using .NET Framework 4.7, C#, Postman.

I make a request with request header Content-Type set to "application/xml". Body looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<BookOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <books>
    <Book>
      <bookName>Buzz</bookName>
      <code>1234</code>
    </Book>
  </books>
</BookOrder>

I thought, out of the box, .NET Framework 4.7 accepts XML requests and will respond in the same way or am I mistaken please?

Instead, I have to set in Global.asax:

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

Why is this? I have tried without setting it, but it won't accept my XML no matter how I format it. Any ideas please?

Thanks

My classes are:

public class BookOrder
{
    public List<Book> books{ get; set; }
}

public class Book
{
    public string bookName { get; set; }
    public string code { get; set; }
}
PKCS12
  • 407
  • 15
  • 41
  • Without that line the default is to use the `DataContractSerializer`. Are you using the data contract attributes? –  Feb 17 '20 at 16:27
  • You are looking at the xml response. The xmlUseSerializer is setting a header in the HTTP Request. I wold use a sniffer like wireshark or fiddler and compare the first request in Postman with the first request in c# to see all the differences. – jdweng Feb 17 '20 at 16:35
  • hi @Amy Is that to add attributes like [DataContract],[XmlSerializerFormat] etc. to every class that I wish to be parsed as XML? If so, will it handle JSON also if the request is content-type = application/json? Thanks! – PKCS12 Feb 17 '20 at 16:45
  • That's a different question. –  Feb 17 '20 at 16:48
  • @Amy Thanks. Fair enough. So, if I use the default DataContractSerializer, I MUST use data contracts in order for it to correctly recognise some of the attributes? Thank you – PKCS12 Feb 17 '20 at 17:44
  • @Questioner - I basically already answered this question [here](https://stackoverflow.com/a/60214943/3744182) for your other question [XML API Request returns “d3p1” for arrays or sub-classes](https://stackoverflow.com/q/60214258/3744182): `DataContractSerializer` automatically assigns an XML namespace when serializing to XML while `XmlSerializer` does not. Since your XML is not in a namespace, if you want to use `DataContractSerializer` you need to override its default XML namespace choice. – dbc Feb 17 '20 at 18:04
  • Thanks @dbc Sorry, struggling to understand the basics of it and I didn't realise your answer for the other question is answers this. – PKCS12 Feb 18 '20 at 09:55

0 Answers0