8

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);
            }
        }
    }
  • Perhaps you could include a code sample – Chris Summers Mar 9 '14 at 19:48
  • The content is client identifiable but added more detail / code above. – Rob Stevenson-Leggett Mar 9 '14 at 19:55
  • 2
    If it's related to the length of the content, perhaps the problem is with your binding(s)? Maybe you need to increase the max allowed length and such. I would expect a different error message, but perhaps it is masked by the one you are getting. – Peter Kjaer Mar 10 '14 at 9:42
  • I've tried upping the limits on both client and server but it doesn't seem to have made any difference - any other ideas? – Rob Stevenson-Leggett Mar 11 '14 at 11:40
  • I've also tried the settings here on the off chance: tridion.stackexchange.com/questions/326/… – Rob Stevenson-Leggett Mar 11 '14 at 11:57
5

Ultimately, this was an encoding issue.

I changed my serialization code from

 public static string Serialize<T>(T obj)
 {
      XmlSerializer xs = new XmlSerializer(typeof(T));
      using (MemoryStream wr = new MemoryStream())
        {
           XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
           ns.Add("", schemaNamespace);
           xs.Serialize(wr, obj, ns);
          return Encoding.UTF8.GetString(wr.GetBuffer());
       }
    }
}

to

    public static string Serialize<T>(T value)
    {
        if (value == null)
        {
            return null;
        }

        XmlSerializer serializer = new XmlSerializer(typeof(T));

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string
        settings.Indent = false;
        settings.OmitXmlDeclaration = false;

        using (StringWriter textWriter = new StringWriter())
        {
            using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
            {
                serializer.Serialize(xmlWriter, value);
            }
            return textWriter.ToString();
        }
    }

and everything worked.

I am not sure why a shorter message worked and a longer did not.

3

I was facing this type of issue while creating a component with content in that. There the problem was with one of the field's content having & in between, so i replaced that with &amp; in code and my problem got solved. May be you can check for any special charector in your rich text field content.

  • My test data doesn't contain any ampersands. Thanks for your answer though. – Rob Stevenson-Leggett Mar 11 '14 at 11:58

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.