I'm trying to import some content into Tridion using the Core Service and have a problem with one of my Rich Text Fields when trying to save a new Component to Tridion.
I'm getting the following error:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://www.sdltridion.com/ContentManager/CoreService/2012:data. The InnerException message was 'There was an error deserializing the object of type Tridion.ContentManager.Data.IdentifiableObjectData. Data at the root level is invalid. Line 5, position 11.'.
The inner exception doesn't really tell me more.
If I delete some of the HTML content it seems to work but I can't see any obvious issues with the deleted part of the content. There's not any special characters in there or broken tags. I'm still trying to narrow it down to a particular part.
If I paste my ComponentData.Content
field into the Source of a Component using the same Schema in the CME the Component saves fine.
I can also paste the problem HTML into the source view of the RTF field and this saves fine.
I initially thought this was an encoding issue but it seems like something else is going on.
Has anyone seen anything similar before?
Update By hardcoding the content of the field of my component I've discovered it seems to be to do with the length of the content..
This saves:
tridionArticle.Content =
"<div xmlns=\"http://www.w3.org/1999/xhtml\"><p xmlns=\"http://www.w3.org/1999/xhtml\">Lorem ipsum Lorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsum</p></div>";
Whereas this does not:
tridionArticle.Content =
"<div xmlns=\"http://www.w3.org/1999/xhtml\"><p xmlns=\"http://www.w3.org/1999/xhtml\">Lorem ipsum Lorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsum</p><p xmlns=\"http://www.w3.org/1999/xhtml\">Lorem ipsum Lorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsum</p><p xmlns=\"http://www.w3.org/1999/xhtml\">Lorem ipsum Lorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsum</p><p xmlns=\"http://www.w3.org/1999/xhtml\">Lorem ipsum Lorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsumLorem ipsum</p></div>";
Here's my code for saving a component:
public void CreateItem(string folderId, TridionNewsArticleModel newsArticleModel)
{
using (var client = new CoreServiceClient(_binding))
{
var schemaFields =
client.ReadSchemaFields(
ConfigurationManager.AppSettings["TridionNewsArticleSchema"], true, null);
var componentData = (ComponentData)client.GetDefaultData(ItemType.Component, folderId, new ReadOptions());
componentData.Title = newsArticleModel.Title;
componentData.Content = XmlSerializationHelper.Serialize(newsArticleModel);
componentData.Schema.IdRef = ConfigurationManager.AppSettings["TridionNewsArticleSchema"];
componentData.Content = HtmlTidy.DecodeHtmlEntities(componentData.Content, false);
//TODO: is there a nicer way to do this?
var metadataFields = Fields.ForMetadataOf(schemaFields, componentData);
metadataFields["author"].Value = newsArticleModel.Author;
metadataFields["pub_date"].Value = newsArticleModel.PubDate.ToString("yyyy-MM-ddTHH:mm:ss");
//TODO: We're going to have to add a flag to the import for this
metadataFields["audience_type"].Value = "Public";
metadataFields["page_title"].Value = newsArticleModel.PageTitle;
metadataFields["page_description"].Value = newsArticleModel.PageDescription;
metadataFields["page_keywords"].Value = newsArticleModel.PageKeywords;
metadataFields["google_page_priority"].Value = newsArticleModel.GooglePagePriority;
metadataFields["google_update_frequency"].Value = newsArticleModel.GooglePagePriority;
componentData.Metadata = metadataFields.ToString();
try
{
client.Create(componentData, null);
ConsoleWriter.Write(ConsoleColor.Green, "Successfully imported " + componentData.Title);
}
catch(Exception ex)
{
ConsoleWriter.Write(ConsoleColor.Yellow, ex.ToString());
_log.Warn(ex);
}
}
}