C# XML Serializable Collection
Asked Answered
G

2

5

I got the below XML (It is just a part of a big XML where I have my problem) that I am trying to make a Serializable class to read the same.

<BANKTRANLIST>
  <DTSTART>20051001</DTSTART>
  <DTEND>20051028</DTEND>
  <STMTTRN> <!-- This element can repeat any number of times -->
    <TRNTYPE>CHECK</TRNTYPE>
    <DTPOSTED>20051004</DTPOSTED>
    <TRNAMT>-200.00</TRNAMT>
  </STMTTRN>
  <STMTTRN>
    <TRNTYPE>ATM</TRNTYPE>
    <DTPOSTED>20051020</DTPOSTED>
    <TRNAMT>-300.00</TRNAMT>
  </STMTTRN>
</BANKTRANLIST>

My C# Implementation

[Serializable]
[XmlRoot("BANKTRANLIST", Namespace = "http://bank.net", IsNullable = false)]
public class BankTransactionList
{
    public BankTransactionList()
    {
        this.StatementTransactions = new List<StatementTransaction>();
    }

    [XmlElement("DTSTART")]
    public string StartDate { get; set; }

    [XmlElement("DTEND")]
    public string EndDate { get; set; }

    [XmlArray("STMTTRN")]
    [XmlArrayItem("STMTTRN")]
    public List<StatementTransaction> StatementTransactions { get; set; }
}


[Serializable]
[XmlRoot("STMTTRN", Namespace = "http://bank.net", IsNullable = false)]
public class StatementTransaction
{
    // TransactionType : ENUM
    [XmlElement("TRNTYPE")]
    public TransactionType TransactionType { get; set; }

    [XmlElement("DTPOSTED")]
    public string DatePosted { get; set; }

    [XmlElement("TRNAMT")]
    public double TransactionAmount { get; set; }
}

My problem is element wrapped again in element which results to get the below output

...
    <STMTTRN> <!-- This does not match my Original XML -->
      <STMTTRN>
        <TRNTYPE>CHECK</TRNTYPE>
        <DTPOSTED>20051004</DTPOSTED>
        <TRNAMT>-200.00</TRNAMT>
      </STMTTRN>
      <STMTTRN>
        <TRNTYPE>ATM</TRNTYPE>
        <DTPOSTED>20051020</DTPOSTED>
        <TRNAMT>-300.00</TRNAMT>
      </STMTTRN>
    </STMTTRN>

Note: Removing [XmlArray("STMTTRN")] tag from List property will not resolve this, instead it will be

If any one can correct me or give me a better solution would be great !!

Gooseneck answered 26/3, 2012 at 9:53 Comment(0)
A
10

Should be [XmlElement] if you want an element per item without a wrapper element:

[XmlElement("STMTTRN")]
public List<StatementTransaction> StatementTransactions { get; set; }
Allout answered 26/3, 2012 at 9:56 Comment(1)
Thanks Mark! I've spent a couple hours today messing around with deriving a class from a collection in order to omit the "parent" name of the collection, trying to then mark up the collection items with specific xml names, then trying to get the collection item itself to omit attributes! This is a simple solution.Thirza
C
0

I would add that in order to serialize the collection you will need to have it something like this:

[Serializable]
[XmlRoot("BANKTRANLIST", Namespace = "http://bank.net", IsNullable = false)]
public class BankTransactionList
{
    public BankTransactionList()
    {
        StatementTransactions = new List<StatementTransaction>()
        {
            new StatementTransaction()
        };
    }

    [XmlElement("DTSTART")]
    public string StartDate { get; set; }

    [XmlElement("DTEND")]
    public string EndDate { get; set; }

    [XmlElement("STMTTRN")]
    public List<StatementTransaction> StatementTransactions { get; set; }
}

Otherwise, without initializing the StatementTransaction object, the list will not be serialized.

Ceremony answered 4/12, 2020 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.