asp.net - Error in formating of XML string with CDATA -
i error "start tag on line 1 not match end tag of 'document'".
string rawxml = "<?xml version='1.0' ?>" + "<document>" + "<![cdata[" + "<topic>" + "my test" + "</topic>" + "]]>" + "</document>";
error occurres when try execute stored procedure send xml parameter.
var xmldoc = new xmldocument(); xmldoc.loadxml(rawxml); dataresultxelement drx = ss.xelem_query(string.format("exec topic_update '{0}', '{1}'", sessionid, xmldoc.innerxml));
if remove works, need cdate store data in database.
should format string differently? thanks!
do not use string manipulation construct xml documents.
do not use string manipulation construct sql queries.
do instead:
stringbuilder sb = new stringbuilder(); using (stringwriter sw = new stringwriter(sb)) using (xmlwriter xw = xmlwriter.create(sw)) { xw.writestartelement("document"); xw.writecdata("<topic>my test </topic>"); xw.writeendelement(); } xdocument result = new xdocument(); using (sqlconnection conn = new sqlconnection(connectionstring)) { conn.open(); sqlcommand cmd = new sqlcommand("topic_update", conn); cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("sessionid", sessionid); cmd.parameters.addwithvalue("xmltext", sb.tostring()); using (xmlreader xr = cmd.executexmlreader()) { result.load(xr); } }
Comments
Post a Comment