Thursday, 18 September 2014

Is there a Standard Way to convert SQLCE Query Results to XML?



I need to query a SQLCE table and convert the result set to XML. The columns of the database:



line_id int
ref_no nvarchar(20)
. . .
new_item int


...have a 1:1 mapping to the required XML:



<INV>
<line_id>1</line_id>
<ref_no>valerie</ref_no>
. . .
<new_item>-1</new_item>
</INV>


I could generate this XML something like this (pseudocode):



int lineId;
String refNum;
. . .
String strXML;
StringBuilder sbXML;
. . . // loop through result set
lineId = resultSet[0];
refNum = resultSet[1];
. . .
strXML = String.Format("<INV><line_id>{0}</line_id><ref_no>{1}</ref_no>. . .<new_item>{2}</new_item></INV>", lineId, refNum, . . .newItem);
sbXML.Add(strXML);


...but this seems rather kludgy. Is there a more elegant solution?


No comments:

Post a Comment