Tridion 2011 Core Service: unable to update component with new field using Xml manipulation

When writing a special import tool for a Tridion 2011 project using the main service, I ran into a problem while trying to save the component.

The following code works fine when the field on the component matters, but when it fails, I get an error.

Here is my code (error handling removed for brevity):

//component is a ComponentData object from Tridion
var doc = new XmlDocument();
doc.LoadXml(component.Content);

var namespaces = new XmlNamespaceManager(doc.NameTable);
namespaces.AddNamespace("ns", doc.DocumentElement.NamespaceURI);

//componentFromSpreadsheet has a dictionary of fields and values to update
foreach (var field in componentFromSpreadsheet.Fields)
{
    XmlNode xmlNode = doc.SelectSingleNode("//ns:" + field.Key, namespaces);

    if (xmlNode == null)
    {
        xmlNode = doc.CreateNode(XmlNodeType.Element, field.Key,
                                 doc.DocumentElement.NamespaceURI);
        doc.DocumentElement.AppendChild(xmlNode);
    }

    //Namespace any Html in the field       
    string fieldValue = HtmlTidy.Tidy(field.Value);
    xmlNode.InnerXml = fieldValue;
}


component.Content = doc.OuterXml;

//This line throws a FaultException<CoreServiceException> with an 
//XmlException from tridion
client.Save(component, null);

Here is a post from Tridion:

"" 'uuid: 09ed2feb-f7cb-4760-ba4c-b9ff4f45d025' 'summary' 'uuid: 09ed2feb-f7cb-4760-ba4c-b9ff4f45d025'. : "related_links" 'UUID: 09ed2feb-f7cb-4760-ba4c-b9ff4f45d025

, .

, Xml. - ?

+5
2

, . . , , , .

+4

( xsd: sequence).

, (, xsd: all ), , , , XSD ( ).

, , .

, FromSpread , , , , , , .

+4

All Articles