gpt4 book ai didi

c# - 将 XML 反序列化为对象 C#

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:08 25 4
gpt4 key购买 nike

我正在尝试将 xml 序列化为对象,但是在序列化之后我的传输协议(protocol)列表是空的。我很确定这很容易,但我找不到我的错误。

我有以下 xml

<transport-agreements type="array">
<transport-agreement>
<id type="integer">1047</id>
<description>Standard</description>
<products type="array">
<product>not important</product>
</products>
</transport-agreement>
<transport-agreement>
<id type="integer">1048</id>
<description>Standard</description>
<products type="array">
<product>not important</product>
</products>
</transport-agreement>
</transport-agreements>

我有以下代码:

var resultContent = await response.Content.ReadAsStreamAsync();
var serializer = new XmlSerializer(typeof(TransportAgreementRoot));
var transportAgreements = (TransportAgreementRoot)serializer.Deserialize(resultContent);

还有我的类(class):

[Serializable, XmlRoot("transport-agreements")]

public class TransportAgreementRoot
{
[XmlArrayItem("transport-agreement")]
TransportAgreement[] TransportAgreements { get; set; }
}

public class TransportAgreement
{
[XmlElement("description")]
public string Description { get; set; }
[XmlElement("id")]
public int Id { get; set; }
[XmlElement("number")]
public string Number { get; set; }
[XmlElement("carrier")]
public Carrier Carrier { get; set; }
[XmlArray("products")]
[XmlArrayItem("product")]
public Product[] Products { get; set; }

}

最佳答案

你可以使用一些在线转换器,它们会给你完整的类图。我最喜欢的是:this one

关于c# - 将 XML 反序列化为对象 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38303156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com