0

I am working on a project where I am saving my data in xml format.I am unable to save double quotes in my data.I have tried replacing double quotes with " but it didn't worked.Please suggest how to achieve this.

Following is the xml data which I am passing

<question HasOptions="0" answer=""I think ki I am vulnerable"...text check // "i am afraid"" QuestionId="2042" OptionId="1260" ></question>

My javascript code for xml creation

function GetXMLForPosting() {

        // here is the XML for posting

        var xmlqQuestionResponse = '<question HasOptions="{{hasOptions}}" answer="{{score}}" QuestionId="{{questionID}}" OptionId="{{optionID}}" />';
        var answers = '';
        //lopping through questions
        $('.little_text').each(function () {

            // question data
            hasOptions = $(this).attr('hasoptions');
            questionID = $(this).attr('questionid');

            // option data
            selectedOption = $(this).find('input[name=radio_' + questionID + ']:checked');
            score = $(selectedOption).attr('value');
            optionID = $(selectedOption).attr('id');

            // end
            if (questionID > 0) {

                if (hasOptions == 0) {
                    // case of textarea
                    // overiding default values
                    selectedOption = $(this).find('textarea[name=radio_' + questionID + ']');
                    score = $(selectedOption).val();
                    optionID = '1260';
                    answers = answers + xmlqQuestionResponse
                            .replace('{{hasOptions}}', hasOptions)
                            .replace('{{score}}', score.replace(/\'/g, "\'"))
                            .replace('{{questionID}}', questionID)
                            .replace('{{optionID}}', optionID);
                }
                else {
                    // normal radio button
                    answers = answers + xmlqQuestionResponse
                            .replace('{{hasOptions}}', hasOptions)
                            .replace('{{score}}', score)
                            .replace('{{questionID}}', questionID)
                            .replace('{{optionID}}', optionID);
                }

            }

        });

        //console.log('<questions>' + answers + '</questions>');
        return '<questions>' + answers + '</questions>';

    }

here my answer is coming in score option

My cs code

[HttpPost]
    public ActionResult Postback(FormCollection formCollection)
    {
        // Process the take questionnaire
        var model = (WorkflowMessage)Session[Enumerations.SessionItems.ViewModel];

        // Check the page guid redirect accoringly
        if (!GuidValid())
            return ReshowPage();

        var rawData = formCollection[Enumerations.FlashVariableFormPostKey.OutputData];
        var enc = Encoding.GetEncoding("ISO-8859-1");
        var responseXml = HttpUtility.UrlDecode(rawData, enc);
        // redundant variable left so that at debug time the result can be inspected.
        var result = new QuestionnaireService().SaveTakenQuestionnaire(PatientSessionDataObject.TreatmentId, model.PatientId, PatientSessionDataObject.CustomerId, model.AssetData, rawData);

        return RedirectToAction("Next", "Treatment", new { navigationChosen = "Next", area = string.Empty, model.PageCheckGuid });
    }

Please suggest how I can save text-"I think ki I am vulnerable"...text check // "i am afraid" in my answer field.Thanks

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
rupinder18
  • 795
  • 1
  • 18
  • 43
  • This previous SO question should help. [escaping - Escape quote character in xml](http://stackoverflow.com/questions/1222367/escape-quote-character-in-xml) – Jason Evans May 01 '15 at 12:19
  • The "XML" you are passing is not XML. That may be the problem, or it may be a typo in your question. – Jon Hanna May 01 '15 at 12:20
  • 1
    I would try very hard *not* to build up XML documents with plain string operations. Use an XML API, so it can do the escaping for you. – Jon Skeet May 01 '15 at 12:21

1 Answers1

0

You need to use &quot;.

For your example...

<question HasOptions="0" answer="&quot;I think ki I am vulnerable&quot;...text check // &quot;i am afraid&quot;" QuestionId="2042" OptionId="1260" ></question>
Yosep Kim
  • 2,931
  • 22
  • 23
  • I have tried using this but its get's saved in similar way in database,should I decode while displaying? – rupinder18 May 01 '15 at 12:26
  • Yeah. Check this out. http://stackoverflow.com/questions/7394748/whats-the-right-way-to-decode-a-string-that-has-special-html-entities-in-it – Yosep Kim May 01 '15 at 12:30