草庐IT

关于 c#:XML 反序列化非基于集合的响应

codeneng 2023-03-28 原文

XML Deserialization For Non-Collection Based Responses

我正在使用 API 来获取有关 Web 应用程序的一些信息。我编写了类来反序列化 XML 响应并添加了 XMLRoot、XMLElement 属性。对于带有子项集合的 XML 响应,我可以使用属性进行反序列化。例如

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<time-entries>
   <time-entry>
        //Other sub nodes
   </time-entry>
   <time-entry>
        //Other sub nodes
   </time-entry>
</time-entries>

对于像上面这样的 XML 响应,我编写了一个 TimeEntry 类,该类具有时间入口节点的其他属性的属性。然后我用 TimeEntry 类的集合编写了另一个类,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
[XmlRoot("time-entries")]
public class TimeEntryResponse
{
    public TimeEntryResponse()
    {

    }

    [XmlElement("time-entry")]
    public List<TimeEntry> TimeEntries { get; set; }

}

因此,使用 TimeEntryResponse 类,我可以反序列化 XML 响应,例如问题的顶部。

但我不能像下面的响应那样反序列化。

1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
<time-totals>
   <total-mins-sum type="integer">382743</total-mins-sum>
   <non-billed-mins-sum type="integer">328988</non-billed-mins-sum>
   <non-billable-hours-sum type="integer">3137.30</non-billable-hours-sum>
</time-totals>

我还为这个响应写了一个 TimeTotal 类。

1
2
3
4
5
6
7
8
9
10
[XmlRoot("time-totals")]
public class TimeTotal
{
    [XmlElement("total-mins-sum")]
    public double TotalMinsSum { get; set; }
    [XmlElement("non-billed-mins-sum")]
    public double NonBilledMinsSum { get; set; }
    [XmlElement("non-billable-hours-sum")]
    public double NonBillableHoursSum { get; set; }
}

然后我写了一个响应类。

1
2
3
4
5
6
7
8
9
public class TimeTotalsResponse : IEntityResponse
{
    public TimeTotalsResponse()
    {

    }

    public TimeTotal TimeTotal { get; set; }
}

如您所见,没有此响应的集合,而且我不知道应该添加响应类 TimeTotal 的哪些属性。

也许我可以直接将 TimeTotal 类的属性放入 TimeTotalResponse 类。但我将使用这个类来反序列化包含时间总计节点的类。

  • 为什么不直接反序列化到 TimeTotal 类?它应该可以工作。或者,如果您还需要实现 IEntityResponseTimeTotalsResponse 子类 TimeTotal 而不是封装它。
  • @dbc 我不想序列化 TimeTotal 类。我需要返回一个从 IEntityResponse 派生的类,因为我在这个接口上使用泛型方法做了很多工作。如果您在评论中有不同的意思,您能用代码告诉它吗?


我不认为您可以使用开箱即用的 XmlSerialization 来做到这一点,因为您的对象不会直接映射到 XML。我能想到两种方法。

方法一:

由于 XML 可以直接反序列化为 TimeTotal,因此首先使用简单的静态方法序列化为 TimeTotal,并将其设置为新创建的 TimeTotalsResponse 的属性,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
static void Main(string[] args)
{
     var response = GetResponse(File.ReadAllText("XMLFile1.xml"));
}

static TimeTotalsResponse GetResponse(string xml)
{
    using (StringReader reader = new StringReader(xml))
    {
        var ser = new XmlSerializer(typeof(TimeTotal));
        return new TimeTotalsResponse() { TimeTotal = (TimeTotal)ser.Deserialize(reader) };
    }
}

方法二:
通过编写自己的 XmlSerializer 来自定义序列化,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class TimeTotalsResponseSerializer : IXmlSerializable
{
    public TimeTotalsResponse Response { get; set; }

    public void ReadXml(XmlReader reader)
    {
        Response = new TimeTotalsResponse();
        Response.TimeTotal = new TimeTotal();

        reader.ReadToDescendant("total-mins-sum");
        Response.TimeTotal.TotalMinsSum = reader.ReadElementContentAsDouble();
        Response.TimeTotal.NonBilledMinsSum = reader.ReadElementContentAsDouble();
        Response.TimeTotal.NonBillableHoursSum = reader.ReadElementContentAsDouble();
    }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void WriteXml(XmlWriter writer)
    {
    }
}

用法是;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
static void Main(string[] args)
{
    XmlReaderSettings settings = new XmlReaderSettings()
    {
        IgnoreWhitespace = true
    };

    string xml = File.ReadAllText("XMLFile1.xml");
    using (XmlReader reader = XmlReader.Create(new StringReader(xml), settings))
    {
        var ser = new TimeTotalsResponseSerializer();
        ser.ReadXml(reader);
        var response = ser.Response;
    }
}

查看问题的最佳方法是对数据进行序列化。请参阅下面的代码。查看为 xsi:type="TimeEntries" 和 xsi:type="TimeTotalsResponse"

生成的两个 XML 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME1 = @"c:\\temp\\test1.xml";
        const string FILENAME2 = @"c:\\temp\\test2.xml";
        static void Main(string[] args)
        {
            TimeEntries timeEntries = new TimeEntries()
            {
                timeEntry = new List<TimeEntry>() {
                    new TimeEntry() {},
                    new TimeEntry() {},
                    new TimeEntry() {}
                }
            };
            TimeTotalsResponse timeTotals = new TimeTotalsResponse()
            {
                TotalMinsSum = new TypeInteger() { _type ="integer", _value = 382743 },
                NonBilledMinsSum = new TypeInteger() { _type ="integer", _value = 328988 },
                NonBillableHoursSum = new TypeInteger() { _type ="integer", _value = 3137.30 }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(TimeEntryResponse));

            StreamWriter writer = new StreamWriter(FILENAME1);
            serializer.Serialize(writer, timeEntries);
            writer.Flush();
            writer.Close();
            writer.Dispose();

            writer = new StreamWriter(FILENAME2);
            serializer.Serialize(writer, timeTotals);
            writer.Flush();
            writer.Close();
            writer.Dispose();


        }
    }
    [XmlInclude(typeof(TimeEntries))]
    [XmlInclude(typeof(TimeTotalsResponse))]
    [Serializable]
    public class TimeEntryResponse
    {

    }
    [XmlRoot("time-entries")]
    public class TimeEntries : TimeEntryResponse
    {
        [XmlElement("time-entry")]
        public List<TimeEntry> timeEntry { get; set; }
    }
    [XmlRoot("time-entry")]
    public class TimeEntry
    {
    }
    [XmlRoot("time-totals")]
    public class TimeTotalsResponse : TimeEntryResponse
    {

        [XmlElement("total-mins-sum")]
        public TypeInteger  TotalMinsSum { get; set; }
        [XmlElement("non-billed-mins-sum")]
        public TypeInteger NonBilledMinsSum { get; set; }
        [XmlElement("non-billable-hours-sum")]
        public TypeInteger NonBillableHoursSum { get; set; }
    }
    public class TypeInteger
    {
        [XmlAttribute("type")]
        public string _type { get; set; }
        [XmlText]
        public double _value { get; set; }
    }
}
a€?

有关关于 c#:XML 反序列化非基于集合的响应的更多相关文章

  1. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  2. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  3. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  4. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  5. ruby - 是否有用于序列化和反序列化各种格式的对象层次结构的模式? - 2

    给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最

  6. 叮咚买菜基于 Apache Doris 统一 OLAP 引擎的应用实践 - 2

    导读:随着叮咚买菜业务的发展,不同的业务场景对数据分析提出了不同的需求,他们希望引入一款实时OLAP数据库,构建一个灵活的多维实时查询和分析的平台,统一数据的接入和查询方案,解决各业务线对数据高效实时查询和精细化运营的需求。经过调研选型,最终引入ApacheDoris作为最终的OLAP分析引擎,Doris作为核心的OLAP引擎支持复杂地分析操作、提供多维的数据视图,在叮咚买菜数十个业务场景中广泛应用。作者|叮咚买菜资深数据工程师韩青叮咚买菜创立于2017年5月,是一家专注美好食物的创业公司。叮咚买菜专注吃的事业,为满足更多人“想吃什么”而努力,通过美好食材的供应、美好滋味的开发以及美食品牌的孵

  7. 基于C#实现简易绘图工具【100010177】 - 2

    C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.

  8. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  9. kvm虚拟机安装centos7基于ubuntu20.04系统 - 2

    需求:要创建虚拟机,就需要给他提供一个虚拟的磁盘,我们就在/opt目录下创建一个10G大小的raw格式的虚拟磁盘CentOS-7-x86_64.raw命令格式:qemu-imgcreate-f磁盘格式磁盘名称磁盘大小qemu-imgcreate-f磁盘格式-o?1.创建磁盘qemu-imgcreate-fraw/opt/CentOS-7-x86_64.raw10G执行效果#ls/opt/CentOS-7-x86_64.raw2.安装虚拟机使用virt-install命令,基于我们提供的系统镜像和虚拟磁盘来创建一个虚拟机,另外在创建虚拟机之前,提前打开vnc客户端,在创建虚拟机的时候,通过vnc

  10. ruby-on-rails - 在 Ruby on Rails 中发送响应之前如何等待多个异步操作完成? - 2

    在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.

随机推荐