c# - Inserting into SQL using an OpenXml -


hi trying insert data sql server database using xml file has data follows.i able attribute mapping in openxml.if try pass xml elements instead of attributes error regarding null insertion.

following xml file (containg attributes)

<newdataset>   <sampledatatable id="20" name="as" address="aaa" email="aa" mobile="123" /> </newdataset> 

i successful using above format.if use below format face errors

<customer>   <id>20</id>   <name>cn</name>   <address>pa</address>   <email>bnso@gmail.com</email>   <mobile>12345513213</mobile> </customer> 

this openxml in sql

 insert @temptable     select * openxml (@xmlhandle,'root/customer/',1)     (cust_id int '@id',           customer_name varchar(30) '@name',           address varchar(30) '@address',           email_id varchar(30) '@email',           mobile_no bigint '@mobile'           )      insert test.dbo.tblcustomers (cust_id,customer_name,address,email,mobile_no) (select * @temptable) 

please help

it's because you're trying fetch data attributes, int xml data inside elements. try this:

insert @temptable select * openxml (@xmlhandle,'root/customer/',1) (cust_id int '@id',       customer_name varchar(30) 'name[1]',       address varchar(30) 'address[1]',       email_id varchar(30) 'email[1]',       mobile_no bigint 'mobile[1]'       ) 

or can without openxml:

select     t.c.value('name[1]', 'varchar(30)') name,     t.c.value('address[1]', 'varchar(30)') address,     t.c.value('email[1]', 'varchar(30)') email,     t.c.value('mobile[1]', 'bigint') mobile @data.nodes('customer') t(c) 

sql fiddle demo


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -