Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I want to insert an xml file into sql table, xml file will looks like the following:
XML
<?xml version="1.0" encoding="UTF-8" ?>
<invoices BatchNo ="01_040115_000009" BatchDate="2016-03-25" type="I">
  <invoice InvoiceMasterID="7576">
                <InvoiceImagePage>
                  <InvoiceImagePageNo>1</InvoiceImagePageNo>
                  <InvoiceImagePath>/Invoices/0000000750.jpg</InvoiceImagePath>
                </InvoiceImagePage>
                <InvoiceImagePage>
                  <InvoiceImagePageNo>2</InvoiceImagePageNo>
                  <InvoiceImagePath>/Invoices/0000000751.jpg</InvoiceImagePath>
                </InvoiceImagePage>
  </invoice>
  <invoice InvoiceMasterID="7577">
                <InvoiceImagePage>
                  <InvoiceImagePageNo>1</InvoiceImagePageNo>
                  <InvoiceImagePath>/Invoices/0000000752.jpg</InvoiceImagePath>
                </InvoiceImagePage>
  </invoice>
</invoices>

I want to parse XML like:

InvoiceMasterID=7576,
InvoiceImagePageNo=1, InvoiceImagePath=/Invoices/0000000750.jpg
InvoiceImagePageNo=2, InvoiceImagePath=/Invoices/0000000751.jpg

&&

InvoiceMasterID=7577,
InvoiceImagePageNo=1, InvoiceImagePath=/Invoices/0000000752.jpg

I want to store this data in database table as InvoiceMasterID wise. like the following,

InvoiceMasterID InvoiceImagePageNo InvoiceImagePath
7576 1 /Invoices/0000000750.jpg
7576 2 /Invoices/0000000751.jpg
7577 1 /Invoices/0000000750.jpg

Please help me

What I have tried:

i have tried through slqbulkcopy code like this
C#
for (int x = 0; x < file.Length; x++)
{
using (SqlConnection con = new SqlConnection(cs))
{
DataSet ds = new DataSet();
ds.ReadXml(file[x].FullName);

DataTable dtinvoices = ds.Tables["invoices"];
DataTable dtinvoice = ds.Tables["invoice "];
DataTable dtEmp = ds.Tables["Employee"];
con.Open();
if(dtinvoices !=null)
{
using (SqlBulkCopy bc = new SqlBulkCopy(con))
{
bc.DestinationTableName = "Departments";
//bc.ColumnMappings.Add("ID", "ID");
bc.ColumnMappings.Add("BatchNo", "BatchNo");
bc.ColumnMappings.Add("BatchDate", "BatchDate");
bc.ColumnMappings.Add("type", "type");
bc.WriteToServer(dtinvoices );
}
}
if(dtinvoice !=null)
{
using (SqlBulkCopy bc = new SqlBulkCopy(con))
{
bc.DestinationTableName = "Departments";
bc.ColumnMappings.Add("InvoiceMasterID", "InvoiceMasterID");
bc.ColumnMappings.Add("InvoiceImagePageNo", "InvoiceImagePageNo");
bc.ColumnMappings.Add("InvoiceImagePath", "InvoiceImagePath");
bc.WriteToServer(dtinvoice );
}
}
}
}

the problem is InvoiceImagePageNo and InvoiceImagePath this nodes not show in dtinvoice tables.

Please guide me.
Posted
Updated 30-Mar-16 21:03pm
v2
Comments
Ram jha 28-Mar-16 6:55am    
hi VR Karthikeyan,
i don't know what do you indicate?
CHill60 28-Mar-16 8:22am    
He's just put <pre> tags around your code to make it easier to read
CHill60 28-Mar-16 8:31am    
Try removing the space in DataTable dtinvoice = ds.Tables["invoice "]; i.e.
DataTable dtinvoice = ds.Tables["invoice"];
AFAIK spaces are not allowed in table names
Ram jha 28-Mar-16 9:01am    
hi,
there are not space that is mistake of copy and paste issue.

1 solution

Hello,

I think you can use XML Reader in order to achieve this. Please follow the below links:
asp.net mvc 3 - How to save xml data to database using mvc4 - Stack Overflow[^]

XML Reader[^]

You can read the data from XML and then save the data. I hope this helps someway.

Thanks
 
Share this answer
 

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