Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

My requirement is to convert few columns in a Datagridview row into XML file. Below is the sample format

XML
<pre lang="xml">
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <namespace:Facturae xmlns:namespace2="http://uri.etsi.org/01903/v1.2.2#" xmlns:namespace3="http://www.w3.org/2000/09/xmldsig#" xmlns:namespace="http://www.facturae.es/Facturae/2007/v3.0/Facturae">


<fileheader> <invoiceissuertype>EM <batch> <batchidentifier>A2800056FBX-375-09 <invoicescount>1 <totalinvoicesamount> <totalamount>7557.48 <totaloutstandingamount> <totalamount>7557.48 <totalexecutableamount> <totalamount>7557.48 <invoicecurrencycode>EUR

<parties>
<sellerparty> <taxidentification> <persontypecode>J <residencetypecode>R <taxidentificationnumber>A2800056F <legalentity>
<corporatename>Public Limited Corporation <addressinspain>
Street Alcala, 137
<postcode>28001 <town>Madrid <province>Madrid <countrycode>ESP

<buyerparty> <taxidentification> <persontypecode>J <residencetypecode>R <taxidentificationnumber>A4155543L <legalentity> <corporatename>Prima S. A. <addressinspain>
Street San Vicente, 1
<postcode>41008 <town>Seville <province>Seville <countrycode>ESP

<parties>






All the data displayed in XML above comes from the selected row data in Datagridview. When user selects the row and clicks a button,row will be converted row to XML format.

If you notice the Parties section it has again got two subsections BuyerParty and SellerParty . Because m completely new to XML would like to know how do we create the above XML format and also with the subsections.

Sample code which i have used for Headers section


C#
List<QuotationMaster> QuotationItemDetails = new List<QuotationMaster>();
            QuotationItemDetails.Add(new QuotationMaster
            {
                TotalQuoteAmount = Convert.ToDecimal(gvQuotationDetails.Rows[QuoteIndex].Cells["TotalQuoteAmount"].EditedFormattedValue),
                TotalOutstandingAmount = Convert.ToDecimal(gvQuotationDetails.Rows[QuoteIndex].Cells["TotalQuoteAmount"].EditedFormattedValue),
                TotalExecutableAmount = Convert.ToDecimal(gvQuotationDetails.Rows[QuoteIndex].Cells["TotalQuoteAmount"].EditedFormattedValue),
                Currency = GlobalData.GetCurrencyCode(Convert.ToInt32(gvQuotationDetails.Rows[QuoteIndex].Cells["CurrencyID"].EditedFormattedValue)),
                ItemDetails = string.Empty
            });
            var xmlPBDetails = new XElement("FileHeader",
                                from pbdetail in QuotationItemDetails
                                select new XElement("Batch",
                                               new XElement("InvoicesCount", 1),
                                               new XElement("TotalInvoicesAmount", pbdetail.TotalQuoteAmount),
                                               new XElement("TotalOutstandingAmount", pbdetail.TotalOutstandingAmount),
                                               new XElement("TotalExecutableAmount", pbdetail.TotalExecutableAmount),
                                               new XElement("InvoiceCurrencyCode", pbdetail.Currency)
                                           ));



Can you please suggest a better way to get the desired results.


Thanks,
Prathap

What I have tried:

Below is the close i have tried and for all the subsections i Have created different methods to get the data inside the main XML.

C#
List<QuotationMaster> QuotationItemDetails = new List<QuotationMaster>();
            QuotationItemDetails.Add(new QuotationMaster
            {
                TotalQuoteAmount = Convert.ToDecimal(gvQuotationDetails.Rows[QuoteIndex].Cells["TotalQuoteAmount"].EditedFormattedValue),
                TotalOutstandingAmount = Convert.ToDecimal(gvQuotationDetails.Rows[QuoteIndex].Cells["TotalQuoteAmount"].EditedFormattedValue),
                TotalExecutableAmount = Convert.ToDecimal(gvQuotationDetails.Rows[QuoteIndex].Cells["TotalQuoteAmount"].EditedFormattedValue),
                Currency = GlobalData.GetCurrencyCode(Convert.ToInt32(gvQuotationDetails.Rows[QuoteIndex].Cells["CurrencyID"].EditedFormattedValue)),
                ItemDetails = string.Empty
            });
            var xmlPBDetails = new XElement("FileHeader",
                                from pbdetail in QuotationItemDetails
                                select new XElement("Batch",
                                               new XElement("InvoicesCount", 1),
                                               new XElement("TotalInvoicesAmount", pbdetail.TotalQuoteAmount),
                                               new XElement("TotalOutstandingAmount", pbdetail.TotalOutstandingAmount),
                                               new XElement("TotalExecutableAmount", pbdetail.TotalExecutableAmount),
                                               new XElement("InvoiceCurrencyCode", pbdetail.Currency)
                                           ));
Posted
Updated 21-Dec-16 15:02pm
v2

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