0

I can de-serialize the XML in my ASP.net Web API project in post request successfully,

XML:

<Request xmlns:i="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://schemas.datacontract.org/2004/07/TestAPI.Models">
 <Child>
  <CountryISO>
   <country xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d4p1:string>CA</d4p1:string>
    <d4p1:string>US</d4p1:string>
   </country>
  </CountryISO>
 </Child>
</Request>

Model

namespace TestWebTuiAPI.Models
{

 public class Request
  {
   public Trip Child{ get; set; }
  }

  public class Child
   {
    public CountryISO CountryISO { get; set; }
   }

    public class CountryISO
     {
         [XmlElement("country")]
         public List<string> country { get; set; }
     }
 }

Now,I need do all the above steps to make my Post Request working in the ASP .Net Web API. If I remove the [XmlElement("country")] attribute from the model, the CountryISO tag in the XML returns null or empty value.

what I want to achieve here is to de-serialize the following XML successfully using POST request,

XML:

<Request>
 <Child>
  <CountryISO>
   <country>CA</country>
   <country>US</country>
  </CountryISO>
 </Child>
</Request>

If I try to post this above XML, I'm getting an invalid request and I'm receiving a null model. I need to add the headers(parent node and CountryISO node) in the first XML to make it work successfully. I've tried various solutions but in vain.

Any advice will be much appreciated.

EDIT: if I use

config.Formatters.XmlFormatter.UseXmlSerializer = true;

I don't need the headers but I'm getting empty values inside the CountryISO tag ?

2 Answers 2

1

If you want to post xml like this:

<Request>
 <Child>
  <CountryISO>
   <country>CA</country>
   <country>US</country>
  </CountryISO>
 </Child>
</Request>

Annotate your properties/classes like the previous answer said, but also add the [XmlElement] attribute to the country property:

[DataContract]
public class Request
{
    [DataMember]
    public Child Child { get; set; }
}

[DataContract]
public class Child
{
    [DataMember]
    public CountryISO CountryISO { get; set; }
}

[DataContract]
public class CountryISO
{
    [DataMember]
    [XmlElement("country")]
    public List<string> country { get; set; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks @peco for your solution but I'm still getting null inside CountryISO tag
Further testing I can make it to work. I don't need header tag for CountryISO, I only the following Heading tag for the XML <Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> . It's not the perfect solution but I'm satisfied with this solution.
0

In WebAPI it is standard practice to mark your classes and properties for serialization operations with [DataContract] and [DataMember] attributes, respectively:

namespace TestWebTuiAPI.Models
{
     [DataContract]
     public class Request
     {
         [DataMember]
         public Trip Child{ get; set; }
     }

     [DataContract]
     public class Child
     {
         [DataMember]
         public CountryISO CountryISO { get; set; }
     }

     [DataContract]
     public class CountryISO
     {
         [DataMember]
         public List<string> country { get; set; }
     }
 }

Without the proper attributes, WebAPI does not know that it should serialize your property.

1 Comment

@ipszBuffer its not working,I still need the XML header <Request xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/TestAPI.Models"> and <country xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> to make the post request work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.