Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there, I was wondering if anyone could help. Im pretty sure its a simple solution but I cant seem to get it working. I have a simple xml file, very simple.

XML
<ResultsDS>
  <Results>
    <Yesno_Vote>NO</Yesno_Vote>
    <PollPosition_1>person1</PollPosition_1>
    <PollPosition_2>person2</PollPosition_2>
    <PollPosition_3>person3</PollPosition_3>
    <PollPosition_4>person4</PollPosition_4>
    <PollPosition_5>person5</PollPosition_5>
  </Results>
</ResultsDS>


i would like to extract the data and put it into relevent columns in my existing sql table.
the table has 7 columns, the 6 named elements in the xml file and an identity column.
and i would essentially like the value of the <YesNo_Vote> element to be inserted into the YesNo_Vote column of my sql table. and so on and so forth.
any help would be much appreciated.
thanks in advance.
Posted
Updated 4-Dec-12 7:47am
v2

1 solution

You can write a Stored Procedure which can do this.
SQL
DECLARE @iDoc INT /* Stores a pointer to the XML document */
DECLARE @XML VARCHAR(MAX) /* Stores the content of the XML */ 
set @XML = '<ResultsDS>
  <Results>
    <Yesno_Vote>NO</Yesno_Vote>
    <PollPosition_1>person1</PollPosition_1>
    <PollPosition_2>person2</PollPosition_2>
    <PollPosition_3>person3</PollPosition_3>
    <PollPosition_4>person4</PollPosition_4>
    <PollPosition_5>person5</PollPosition_5>
  </Results>
</ResultsDS>'
EXEC sp_xml_preparedocument @iDoc OUTPUT, @XML
INSERT INTO YourTable
SELECT *
FROM OPENXML(@iDoc,'/ResultsDS/Results',2)
WITH (Yesno_Vote varchar(50)  'Yesno_Vote',
PollPosition_1 varchar(50)  'PollPosition_1',
PollPosition_2 varchar(50)  'PollPosition_2',
PollPosition_3 varchar(50)  'PollPosition_3',
PollPosition_4 varchar(50)  'PollPosition_4',
PollPosition_5 varchar(50)  'PollPosition_5'
)
EXEC sp_xml_removedocument @iDoc


I assumed the data types to be varchar(50), change them according to your table's properties.
 
Share this answer
 
v2
Comments
sarahmccarthy 4-Dec-12 15:55pm    
hi there, thanks for that, i am VERY new to this stuff so im sorry for this next part. but how do i use a stored procedure exactly? its not something ive used before. Thanks again for your input
Teenustar 4-Dec-12 16:05pm    
If you are referring to writing a stored Procedure, then refer to the link http://www.codeproject.com/Articles/126898/Sql-Server-How-to-write-a-Stored-procedure-in-Sql or if you are referring to handle the stored proc in .net, then learn ADO.NET using the following link http://msdn.microsoft.com/en-us/library/kb9s9ks0.aspx
sarahmccarthy 5-Dec-12 5:35am    
Thanks for that, ill look at those now. thanks again.
sarahmccarthy 6-Dec-12 7:42am    
Cheers guys, got it working. :) thanks for the help.

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