Create repeating elements in xml document from datatable

chester

New member
Joined
Apr 29, 2015
Messages
1
Programming Experience
1-3
Hi all,

I am creating an XML document from a schema. I used xsd.exe to create a class file from the xsd. I have a datatable that I am using to write the xml with. Below is a sample with only first name and surname.

As expected though, 5 nodes are created, the problem is that it only writes the last item from the datatable. What do I need to change?

C#:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
 
                XmlSerializer serializer = new XmlSerializer(typeof(UKFATCASubmissionFIReport));
                TextWriter writer = new StreamWriter(destinationPath);
                UKFATCASubmissionFIReport fatcaSub = new UKFATCASubmissionFIReport();
                fatcaSub.SchemaVersion = schemaVersion;
 
                //Initialize classes and objects
                #region classes and objects
 
                AccountDataType accountData = new AccountDataType();
                AccountHolderCodeType accountHolderType = new AccountHolderCodeType();
                MessageDataType messageData = new MessageDataType();
                MonetaryType moneyType = new MonetaryType();
                SubmissionType submissionData = new SubmissionType();
                FIReturnActionType fireturnActionData = new FIReturnActionType();
                AccountActionType accountActionData = new AccountActionType();
                TINCodeType tinCode = new TINCodeType();
                HolderTaxInfoType holderInfo = new HolderTaxInfoType();
                //PaymentDataType paymentData = paymentDetails();
                
                
                #endregion
 

                //Monetary type
                moneyType.Value = moneyTypeValue;
                moneyType.currCode = currCode_Type.GBP;
 
                //Create message data
                messageData.FATCAUserId = FatcaUserID;
                messageData.XMLTimeStamp = timeStamp;
                messageData.MessageCategory = MessageType.NewSubmission;
                fatcaSub.MessageData = messageData;
 
                //Create submission data object
                submissionData.ReportingPeriod = reportingPeriod;
                submissionData.Item = messageRef;
                fatcaSub.Item = submissionData;
 
                //FI Return Action

                fireturnActionData.Action = ActionType.New;
                fireturnActionData.FIReturnRef = returnRef;
 
                //FIReturnActionType[] fireturnItems = new FIReturnActionType[] { fireturnActionData };
                //TIN Code Type
                tinCode.TINCountryCode = countryCode;
                tinCode.Value = tinCodeValue;
                tinCode.TINCountryCodeSpecified = itemSpecified;
 
                //TIN Information
                holderInfo.ReportableJurisdiction = countryCode;
                holderInfo.TIN = holderTIN;
                holderInfo.TINCode = tinCode;
 
                //ContactPersonInformation contactInfo = ContactInformation(holderInfo);
                //Contact address
                ContactAddressType contactAddress = new ContactAddressType();
                contactAddress.StreetName = streetName;
                contactAddress.City = contactCity;
                contactAddress.CountryCode = holderInfo.ReportableJurisdiction;
 
                //Person Details
                ContactPersonInformation contactInfo = new ContactPersonInformation();
                //contactInfo.FirstName = firstName;
                contactInfo.LastName = lastName;
                contactInfo.Address = contactAddress;
                contactInfo.HolderTaxInfo = holderInfo;
                contactInfo.BirthDateSpecified = itemSpecified;
                contactInfo.BirthDate = dateOfBirth;
 
                PaymentMonetaryType paymentMonetary = new PaymentMonetaryType();
                paymentMonetary.currCode = currCode_Type.GBP;
                paymentMonetary.Value = paymentValue;
 
                //Payment data object
                PaymentDataType paymentData = new PaymentDataType();
                paymentData.PaymentCode = PaymentCodeType.Item20;
                paymentData.PaymentAmount = paymentMonetary;
                //return paymentData;

                //Account action
                accountActionData.AccountRef = accountRef;
                accountActionData.Action = ActionType.New;
              
                //Account information array
                //AccountData(paymentData, contactInfo, accountActionData, accountData, accountHolderType);

                object[] accountItems = new object[] { accountActionData, accountNumber, paymentData, accountHolderType, contactInfo };
                accountData.Items = accountItems;
 
                ItemsChoiceType[] accountFieldNames = new ItemsChoiceType[] { ItemsChoiceType.AccountAction, ItemsChoiceType.AccountNumber, ItemsChoiceType.PaymentData, ItemsChoiceType.AccountHolderType, ItemsChoiceType.Person };
                accountData.ItemsElementName = accountFieldNames;
 
                DataTable dtTable = new DataTable();
                dtTable.Columns.Add("FirstName", typeof(string));
                dtTable.Columns.Add("Surname", typeof(string));
 
                // Here we add five DataRows.
                dtTable.Rows.Add("Test", "Test");
                dtTable.Rows.Add("Two", "Three");
                dtTable.Rows.Add("Four", "Five");
                dtTable.Rows.Add("Six", "Seven");
                dtTable.Rows.Add("Eight", "Nine");
 

                List<FIReturnType> fiRetList = new List<FIReturnType>();
                foreach (DataRow row in dtTable.Rows)
 
                {
                    object fName = row["FirstName"];
                    object sName = row["Surname"];
                    contactInfo.FirstName = fName.ToString();
                    contactInfo.LastName = sName.ToString();
                   
                    FIReturnType fireturn = new FIReturnType();
                    object[] fiItems = new object[] { fireturnActionData, fiRegisterID, dueDilligenceInd, thresholdInd, accountData };
                    fireturn.Items = fiItems;
 
                    ItemsChoiceType1[] fiFieldnames = new ItemsChoiceType1[] { ItemsChoiceType1.FIReturnAction, ItemsChoiceType1.FIRegisterId, ItemsChoiceType1.DueDiligenceInd, ItemsChoiceType1.ThresholdInd, ItemsChoiceType1.AccountData };
                    fireturn.ItemsElementName = fiFieldnames;
 
                    fiRetList.Add(new FIReturnType { Items = fiItems, ItemsElementName = fiFieldnames });
 
                    FIReturnType[] fiData = fiRetList.ToArray();
                
                submissionData.FIReturn = fiData;
}
                //Serialize the submission close the TextWriter

                serializer.Serialize(writer, fatcaSub);
                
                writer.Close();
 
Back
Top Bottom