【问题标题】:C# ASP.NET CORE WEB API headless ARRAY deserialization struggleC# ASP.NET CORE WEB API headless ARRAY 反序列化之争
【发布时间】:2022-11-02 16:44:43
【问题描述】:

我很难构建用于反序列化 SOAP 响应的 DATACONTRACT 类:

     <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>

我尝试下一个课程:

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

并使用自定义集合:

    [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> { }

问题是对象 <MATCHES> 有 <ITEM> 和 <PAGE> 对象的列表。并且 <ITEM> 列表没有像 <ITEMS> 这样的 wrapt 元素

我得到的结果是:

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

反序列化程序获取 <PAGE> 和 <STATUS> 但项目元素未填充数据

这是我的 searchMatch 和 MatchesPage 类:

    [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; }
    }

请帮帮我……我想回家……

我现在用谷歌搜索了 3 天

【问题讨论】:

  • 什么是完整的 XML 响应?响应是否有命名空间?
  • 服务器上的数据合同有命名空间,我在课堂上展示它们,为了隐私,他们用“blah blah blah”屏蔽了它们。
  • 没有所有信息,我无法判断出什么问题。该问题可能是由许多不同的事情错误引起的。我不想走错路。
  • <searchRegistryItemResponse> <status></status> <matches> <item> <emdrId></emdrId> <localUid></localUid> <registrationDate></registrationDate> <registrationDateTime></registrationDateTime> <storeTillDate></storeTillDate > </item> <item> <emdrId></emdrId> <localUid></localUid> <registrationDate></registrationDate> <registrationDateTime></registrationDateTime> <storeTillDate></storeTillDate> </item> <page> <itemsPerPage></itemsPerPage> <hasNext></hasNext> </page> </matches> </searchRegistryItemResponse>
  • 您刚刚发布的内容中没有命名空间。您的类具有您可能需要删除的名称空间。

标签: c# soap deserialization datacontract


【解决方案1】:

代码应如下所示

    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
    {
    }

【讨论】:

    猜你喜欢
    • 2021-12-17
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多