Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.

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

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();
Posted
Comments
gggustafson 30-Apr-15 0:10am    
we don't need to see your whole program or large portions of it. use improve question to reduce the amount of code to just what's required to explain your problem.
Sinisa Hajnal 30-Apr-15 6:26am    
Change object fName = row["FirstName"] to string fName etc...

Limit the code to what is relevant.
Add try..catch..finally around the code, set some message box in catch to see if there are errors and dispose of disposables in finally.

Fatca is dangerous (i.e.expensive) system to create poorly. I'd advise you to hire a professional programmer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900