gpt4 book ai didi

c# - 使用重复元素反序列化 XML

转载 作者:行者123 更新时间:2023-12-02 17:31:59 25 4
gpt4 key购买 nike

这是 VS2012 中的 C#,针对 .NET 4.5 构建

我是 XML 序列化/反序列化的新手,正在尝试解决这个问题。我有 XML

<?xml version="1.0"?>
<AvailabilityResponse>
<ApiKey>LZ6c@3O9#tq*BAyX4KGYBsCgZ*HpUDtrB*XI*WGLw</ApiKey>
<ResellerId>101</ResellerId>
<SupplierId>1004</SupplierId>
<ForeignReference>1234567890</ForeignReference>
<Timestamp>2015-08-06T05:20:49.000Z</Timestamp>
<RequestStatus>
<Status>SUCCESS</Status>
</RequestStatus>
<TTAsku>dcnt</TTAsku>
<TourAvailability>
<TourDate>2015-08-31</TourDate>
<TourOptions>
<DepartureTime>07:30 PM</DepartureTime>
</TourOptions>
<AvailabilityStatus>
<Status>AVAILABLE</Status>
</AvailabilityStatus>
</TourAvailability>
<TourAvailability>
<TourDate>2015-08-31</TourDate>
<TourOptions>
<DepartureTime>08:30 PM</DepartureTime>
</TourOptions>
<AvailabilityStatus>
<Status>AVAILABLE</Status>
</AvailabilityStatus>
</TourAvailability>
</AvailabilityResponse>

我正在尝试反序列化为此类结构:

[Serializable]
public class AvailabilityResponse
{
public string ApiKey { get; set; }
public string ResellerId { get; set; }
public string SupplierId { get; set; }
public string ForeignReference { get; set; }
public DateTime Timestamp { get; set; }
public RequestStatus RequestStatus { get; set; }
public string TTAsku { get; set; }
public TourAvailability[] TourAvailability { get; set; }
}

[Serializable]
public class RequestStatus
{
public string Status { get; set; }
}

[Serializable]
public class TourAvailability
{
public DateTime TourDate { get; set; }
public TourOptions TourOptions { get; set; }
public AvailabilityStatus AvailabilityStatus { get; set; }
}
[Serializable]
public class AvailabilityStatus
{
public string Status { get; set; }
public string UnavailabilityReason { get; set; }
}

我这样做是为了:

public static AvailabilityResponse DeserializeAvailabilityResponse(Stream replyStream)
{
XmlSerializer xmlSr = null;
XmlReader inreader = null;

if (replyStream != null)
{
XmlTextReader xmlreader = new XmlTextReader(replyStream);
XmlDocument respXml = new XmlDocument();
respXml.Load(xmlreader);
xmlreader.Close();

xmlSr = DeserializeXmlDoc(respXml, out inreader, typeof(AvailabilityResponse));
}

if (xmlSr != null && inreader != null)
{
AvailabilityResponse inventory = (AvailabilityResponse)xmlSr.Deserialize(inreader);
return inventory;
}
return null;
}

问题是,当我检查返回的库存项目时,TourAvailability 如下所示:
enter image description here
我希望它类似于 RequestStatus,例如,使用 [+] 允许我打开它并查看每个元素。即使我歪曲了它,我也希望至少有一个 TourAvailability,而不是零。

我可能在这里遗漏了几件事,但非常感谢您提供的任何帮助。我还有很多这类事情要处理,因为我们公司正在稍微改变方向。

最佳答案

只需使用 [XmlElement("TourAvailability")] 属性,然后您就会看到数组的元素。

public class AvailabilityResponse
{
public string ApiKey { get; set; }
public string ResellerId { get; set; }
public string SupplierId { get; set; }
public string ForeignReference { get; set; }
public DateTime Timestamp { get; set; }
public RequestStatus RequestStatus { get; set; }
public string TTAsku { get; set; }
[XmlElement("TourAvailability")]
public TourAvailability[] TourAvailability { get; set; }
}

顺便说一句:你不需要那些[Serializable]属性

PS:你的反序列化代码可以简化为:

using (var f = File.OpenRead(filename))
{
XmlSerializer ser = new XmlSerializer(typeof(AvailabilityResponse));
var resp = (AvailabilityResponse)ser.Deserialize();
}

关于c# - 使用重复元素反序列化 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31860204/

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