列出

时间:2017-12-01 08:47:03

标签: .net xml xmlserializer

我有以下XML,我需要将“DUE”和“RATE”映射到带有XmlSerializer的对象列表。可以有零到多,并且它们总是以相同的“idx”形式出现。

<INVOICE ID="4">
    <STATUS>S</STATUS>
    <TOTAL>6230.00</TOTAL>
    <DUE idx="1">14.12.17</DUE>
    <RATE idx="1">6230.00</RATE>
</INVOICE >
<INVOICE ID="5">
    <STATUS>S</STATUS>
    <TOTAL>3270.00</TOTAL>
    <DUE idx="1">30.11.17</DUE>
    <RATE idx="1">1090.00</RATE>
    <DUE idx="2">07.12.17</DUE>
    <RATE idx="2">1090.00</RATE>
    <DUE idx="3">14.12.17</DUE>
    <RATE idx="3">1090.00</RATE>
</INVOICE>

我有以下设置,没有“Rate”和“Due”列表正常工作:

[Serializable]
public class UserInvoicesDto
{
    [XmlElement("INVOICE")]
    public List<UserInvoiceDto> Invoices { get; set; }
}

[Serializable, XmlRoot("INVOICE")]
public class UserInvoiceDto
{
    [XmlAttribute("id")]
    public int InvoiceId { get; set; }
    [XmlElement("TOTAL")]
    public string Total { get; set; }
}

然后我有以下课程。

[Serializable]
public class InvoicesDueDates
{
    [XmlAttribute("idx")]
    public string Id { get; set; }
    [XmlElement("DUE")]
    public string DueDate { get; set; }
    [XmlElement("RATE")]
    public string Rate { get; set; }
}

有可能吗?

1 个答案:

答案 0 :(得分:1)

如果您只需反序列化,则可以使用XmlSerializer对以下类型执行此操作:

[XmlRoot(ElementName = "DUE")]
public class DueDTO
{
    [XmlAttribute(AttributeName = "idx")]
    public string Idx { get; set; }
    [XmlText]
    public string Text { get; set; }
}

[XmlRoot(ElementName = "RATE")]
public class RateDTO
{
    [XmlAttribute(AttributeName = "idx")]
    public string Idx { get; set; }
    [XmlText]
    public decimal Text { get; set; }
}

[XmlRoot(ElementName = "INVOICE")]
public partial class InvoicesDTO
{
    [XmlAttribute(AttributeName = "ID")]
    public string Id { get; set; }
    [XmlElement(ElementName = "STATUS")]
    public string Status { get; set; }
    [XmlElement(ElementName = "TOTAL")]
    public decimal Total { get; set; }

    [XmlElement(ElementName = "DUE")]
    public List<DueDTO> Due { get; set; }
    [XmlElement(ElementName = "RATE")]
    public List<RateDTO> Rate { get; set; }
}

然后,要将RateDue列表合并到一个InvoicesDueDates集合中,您可以使用LINQ,例如如下:

public partial class InvoicesDTO
{
    public InvoicesDueDates[] InvoicesDueDates
    {
        get
        {
            // To make suure we handle cases where only a Rate or Due item of a specific index is present,
            // perform left outer joins with all indices on both Rate and Due.
            // https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-left-outer-joins
            var query = from i in Due.Select(d => d.Idx).Concat(Rate.Select(r => r.Idx)).Distinct()
                        join due in Due on i equals due.Idx into dueGroup
                        // Throw an exception if we have more than one due item for a given index
                        let due = dueGroup.SingleOrDefault()
                        join rate in Rate on i equals rate.Idx into rateGroup
                        // Throw an exception if we have more than one rate item for a given index
                        let rate = rateGroup.SingleOrDefault()
                        select new InvoicesDueDates { Id = i, DueDate = due == null ? null : due.Text, Rate = rate == null ? (decimal?)null : rate.Text };
            return query.ToArray();
        }
    }
}

public class InvoicesDueDates
{
    public string Id { get; set; }
    public string DueDate { get; set; }
    public decimal? Rate { get; set; }
}

注意:

  • 此解决方案利用了以下事实:当XmlSerializer反序列化List<T>属性并遇到与其他元素交错的列表元素时,它将追加每个列表元素遇到了不断增长的列表。

  • 如果您重新序列化InvoicesDTO,结果将如下所示:

      <INVOICE ID="5">
        <STATUS>S</STATUS>
        <TOTAL>3270.00</TOTAL>
        <DUE idx="1">30.11.17</DUE>
        <DUE idx="2">07.12.17</DUE>
        <DUE idx="3">14.12.17</DUE>
        <RATE idx="1">1090.00</RATE>
        <RATE idx="2">1090.00</RATE>
        <RATE idx="3">1090.00</RATE>
      </INVOICE>
    

    请注意,所有信息都已保留并重新序列化,但<RATE><DUE>序列已被分开。

  • 如果您需要使用交错的<RATE><DUE>元素重新序列化,则必须采用其他策略,例如serializing a list of KeyValuePair to XML或{{ 3}}

  • 我使用Xml Sequence deserialization with RestSharp自动生成DTO类,然后对其进行修改以符合我的命名争用。

示例工作https://xmltocsharp.azurewebsites.net/