Search code examples
c#soapdeserializationdatacontract

C# ASP.NET CORE WEB API headless ARRAY deserialization struggle


i have a strugle to construct DATACONTRACT class for deserialization of SOAP response:

     <matches>
        <item>
          <emdrId>66.19.121.000000301</emdrId>
          <localUid>3ece0f3e-f691-4a6d-bc16-4f34805d97d8</localUid>
          <registrationDate>2019-02-08T00:00:00+03:00</registrationDate>
          <registrationDateTime>2019-02-08T10:40:00.612+03:00</registrationDateTime>
          <storeTillDate>2039-01-30T21:00:00+00:00</storeTillDate>
        </item>
        <item>
          <emdrId>66.22.5036.001854228</emdrId>
          <localUid>86689ddd-597b-4de4-af07-f565713635ab</localUid>
          <registrationDate>2022-10-18T00:00:00+03:00</registrationDate>
          <registrationDateTime>2022-10-18T17:07:11.632+03:00</registrationDateTime>
          <storeTillDate>2047-10-17T21:00:00+00:00</storeTillDate>
        </item>
        <page>
          <itemsPerPage>1000</itemsPerPage>
          <hasNext>false</hasNext>
        </page>
      </matches>

I try next clases:

    [DataContract(Namespace = "blah blah blah")]
    public class SearchMatches
    {
        [DataMember] public searchMatch[]? item { get; set; }
        [DataMember] public MatchesPage? page { get; set; }
    }

And with custom collection:

    [DataContract(Namespace = "blah blah blah")]
    public class SearchMatches
    {
        [DataMember] public ArrayOfsearchMatch? item { get; set; }
        [DataMember] public MatchesPage? page { get; set; }
    }

    [CollectionDataContract(ItemName = "item", Namespace = "blah blah blah")]
    public class ArrayOfsearchMatch : List<searchMatch> { }

The problem is object <MATCHES> have list of <ITEM> and <PAGE> object. And list of <ITEM> didnt have wrapt element like <ITEMS>

All i have in results is:

{
  "status": "success",
  "matches": {
    "item": [],
    "page": {
      "itemsPerPage": 1000,
      "hasNext": false
    }
  },
  "errors": null
}

Deserializer get <PAGE> and <STATUS> but item element didnt filled with data

Heres my searchMatch and MatchesPage clases:

    [DataContract(Namespace = "blah blah blah")]
    public class searchMatch
    {
        [DataMember] public string? emdrId { get; set; }
        [DataMember] public string? localUid { get; set; }
        [DataMember] public DateTimeOffset? registrationDate { get; set; }
        [DataMember] public DateTimeOffset? registrationDateTime { get; set; }
        [DataMember] public DateTime? storeTillDate { get; set; }
    }

    [DataContract(Namespace = "blah blah blah")]
    public partial class MatchesPage : object
    {
        [DataMember] public int itemsPerPage { get; set; }
        [DataMember] public bool hasNext { get; set; }
    }

Plz help me.... i wanna go home......

I googled like 3 days at now


Solution

  • Code should look like this

        public class Root
        {
            [XmlElement("matches")]
            public Matches matches { get; set; }
        }
        public class Matches
        {
            [XmlElement("item")]
            public Item[] item { get; set; }
    
            [XmlElement("page")]
            public Page page { get; set; }
        }
        public class Item
        {
        }
        public class Page
        {
        }