3

I want to save content with HTML tags in a normal text field with the Core Service. It works for an RTF field type, but the normal field type throws this exception:

XML validation error. Reason: The element 'uuid:9825cfbb-ca50-4619-91ee-4d2ff2eb216e:reference' cannot contain child element 'uuid:9825cfbb-ca50-4619-91ee-4d2ff2eb216e:b' because the parent element's content model is text only.

<referenced-text xmlns=\"uuid:9825cfbb-ca50-4619-91ee-4d2ff2eb216e\">
  <quote><p>this is RTF...Imagination is more important than knowledge</p></quote>
  <reference><b>Normal text field...Fails...A. Author again</b></reference>
</referenced-text>

Any ideas?

5

I think you have to HTML Encode the text before you save it and HTML Decode when you retrieve it

4

If you can consider your HTML fragment as an XML, you may also use the CDATA in addition to what Eric has suggested. You may use following codeto get an idea (There may be better implementation possible):

        private static void CDatafyXMLNode(XmlDocument doc, XmlNode thenode)
        {
            XmlNode cdata_node = doc.CreateCDataSection(thenode.InnerXml);
            thenode.RemoveAll();
            thenode.AppendChild(cdata_node);
        }

        public static string StripCdata(string input)
        {
            if (input == null)
                return String.Empty;

            String output = input;

            if (input.IndexOf("<![CDATA[") == 0)
            {
                output = input.Substring(9, input.Length - 9);
                if (output.IndexOf("]]>") == output.Length - 3)
                    output = output.Substring(0, output.Length - 3);
            }

            return output;
        }

Performance wise it would be almost similar to what HTML Encode/Decode does (or it may be better not sure though) but one advantage it provides is the Simplicity and readability while debugging or saving.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.