I want to save records fetched from database in an XML file,
take x number of records from XML file into a custom collection List
process them and
List choiceSet = new List();
choiceSet = biEntityObj.ChoiceSet;
XmlDocument ChoiceXML = new XmlDocument();
ChoiceXML.AppendChild(ChoiceXML.CreateElement("CHOICESET"));
foreach (var item in choiceSet)
{
XmlElement element = ChoiceXML.CreateElement("CHOICE");
// element.AppendChild(ChoiceXML.CreateElement("CHOICE_ID")).InnerText = Convert.ToString(item.ChoiceID);
element.AppendChild(ChoiceXML.CreateElement("CHOICE_TEXT")).InnerText = Convert.ToString(item.ChoiceText);
element.AppendChild(ChoiceXML.CreateElement("SEQUENCE")).InnerText = Convert.ToString(item.Sequence);
element.AppendChild(ChoiceXML.CreateElement("ISCORRECT")).InnerText = Convert.ToString(item.IsCorrect);
ChoiceXML.DocumentElement.AppendChild(element);
}
Pass ChoiceXML to Stored procedure then SQL Server Do like as below
@Choice_XML VARCHAR(MAX)=NULL
IF(@Choice_XML<>'')
BEGIN
SET @intDocHandle =0
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @intDocHandle OUTPUT,
@Choice_XML
--SET @ChoiceID = (SELECT max([choice_id])+1 AS 'ChoiceID' from BI_QUESTION_CHOICE)
--Insert
INSERT BI_QUESTION_CHOICE
(
[choice_id],
[choice_descr],
[sequence],
[question_id],
[is_correct],
[created_by],
[created_dt],
[modified_by],
[modified_dt]
)
SELECT (SELECT max([choice_id])+1 AS 'ChoiceID' from BI_QUESTION_CHOICE),
CASE WHEN CHOICE_TEXT='' THEN NULL ELSE CHOICE_TEXT END,
CASE WHEN SEQUENCE='' THEN NULL ELSE SEQUENCE END,
QuestionID,
CASE WHEN ISCORRECT='' THEN NULL ELSE ISCORRECT END,
'mbathini',
GETDATE(),
'mbathini',
GETDATE()
FROM OPENXML(@intDocHandle,'/CHOICESET/CHOICE', 3)
WITH
(CHOICE_TEXT VARCHAR(500),
SEQUENCE VARCHAR(50),
QuestionID INT,
ISCORRECT bit)
END