0

Hi i have the Following XML file.how do i read it and insert the data in a table using a stored procedure

<NewDataSet>
<Root RowNumber=1; answer = 1; TAnswer=null/>
<Root RowNumber=2; answer = 6; TAnswer=yes for Q 2/>
<Root RowNumber=3; answer = 9; TAnswer=null/>
<Root RowNumber=4; answer = -1; TAnswer=q 4 no suggestions/>
</NewDataSet>
6
  • What programming language are you using? What have you tried to parse the data from XML? Commented Jun 19, 2017 at 5:34
  • @BhavikShah we are using c# Commented Jun 19, 2017 at 5:35
  • This is the format that i will get the file in need to read it and store it in the table Commented Jun 19, 2017 at 5:36
  • Check this Commented Jun 19, 2017 at 5:37
  • That isn't proper XML unless you copy pasted it incorrectly without quotation marks. Commented Jun 19, 2017 at 5:38

1 Answer 1

1

Considering you have a valid xml just like the one below.

DECLARE @xml XML

SET @xml = '
<NewDataSet>
<Root RowNumber = "1" answer = "1" TAnswer = "null" />
<Root RowNumber = "2" answer = "6" TAnswer = "yes for Q 2" />
<Root RowNumber = "3" answer = "9" TAnswer = "null" />
<Root RowNumber = "4" answer = "-1" TAnswer = "q 4 no suggestions" />
</NewDataSet>'

SELECT RowNumber = T.A.value('@RowNumber', 'int'),
       answer = T.A.value('@answer', 'int'),
       TAnswer = T.A.value('@TAnswer', 'varchar(1000)')
FROM   @xml.nodes('//NewDataSet/Root') T (A) 

Note : There are two mistakes in your XML. Attributes values are not enclosed by double quotes. Then the attributes should be separated by space not by semi-colon

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks your output helped.Got what i was looking for.Actually the xml was provided by the developer and i had to read and insert it into the table.So asked the developer to make the changes accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.