Serializing a list of objects

TRAIAN MACAVEIU 91 Reputation points
2024-04-18T09:03:26.0133333+00:00

Hello people!
I have a structure of the form:

public class MasterFiles
		{
			public GeneralLedgerAccounts GeneralLedgerAccounts { get; set; }
			public Customers Customers { get; set; }
			public Suppliers Suppliers { get; set; }
			public TaxTable TaxTable { get; set; }
			public UOMTable UOMTable { get; set; }
			public AnalysisTypeTable AnalysisTypeTable { get; set; }
			public string MovementTypeTable { get; set; }
			public Products Products { get; set; }
			public string Owners { get; set; }
			public string Assets { get; set; }
		}
public class Account
		{
			public string AccountID { get; set; }
			public string AccountDescription { get; set; }
			public string StandardAccountID { get; set; }
			public string AccountType { get; set; }
			public string AccountCreationDate { get; set; }
			public string OpeningCreditBalance { get; set; }
			public string ClosingCreditBalance { get; set; }
			public string OpeningDebitBalance { get; set; }
			public string ClosingDebitBalance { get; set; }
		}
public class GeneralLedgerAccounts
		{		
			public List<Account> Account { get; set; }
		}

data completion:

GeneralLedgerAccounts gla = new GeneralLedgerAccounts();
gla.Account = new List<Account>();
gla.Account.Add(new Account()
            {
                AccountID = "1", ...
            });
masterfiles.GeneralLedgerAccounts = gla;

after serialization, I get an XML file with the following structure:

<nsSAFT:MasterFiles>
    <nsSAFT:GeneralLedgerAccounts>
      <nsSAFT:Account>
        <nsSAFT:Account>
          <nsSAFT:AccountID>1</nsSAFT:AccountID>
		</nsSAFT:Account>
      </nsSAFT:Account>
    </nsSAFT:GeneralLedgerAccounts>dgerAccounts>
</nsSAFT:MasterFiles>

what am I doing wrong because the desired structure should look like this:

<nsSAFT:MasterFiles>
	<nsSAFT:GeneralLedgerAccounts>
      <nsSAFT:Account>
          <nsSAFT:AccountID>1</nsSAFT:AccountID>
      </nsSAFT:Account>
	</nsSAFT:GeneralLedgerAccounts>
</nsSAFT:MasterFiles>

Thank you for the answers!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,266 questions
{count} votes

Accepted answer
  1. David Lowndes 2,350 Reputation points MVP
    2024-04-18T09:10:02.0066667+00:00

    The Account element under GeneralLedgerAccounts is presumably because its the name of the member of GeneralLedgerAccounts. Perhaps if you change GeneralLedgerAccounts to derive from a List<Account> rather than have a member of that type, that will give you the structure you want.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful