Asp.net WebAPI XML deserialize?

2019-09-14 01:44发布

问题:

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 ?

回答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; }
}


回答2:

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.