I am updating a Component using Core Service in Tridion 2011.
The sample code is as follows,
string COMPONENT_URI = "tcm:8-674";
string SCHEMA_URI = "tcm:8-426-8";
ComponentData component = client.TryCheckOut(COMPONENT_URI, null) as ComponentData;
try
{
Response.Write("<BR>" + component.Content);
XDocument xdoc = XDocument.Parse(component.Content);
var element = xdoc.Elements("first").Single();
element.Value = "updated";
xdoc.Save(component.Content);
client.Save(component, null);
Response.Write("<BR"+"SAVED");
}
catch (Exception ex)
{
Response.Write("Unable to save comp" + ex.Message);
}
client.CheckIn(COMPONENT_URI, null);
I am getting following exception:
Unable to save compSequence contains no elements
Details:
first – name of the field in the component
Can any one help regarding this?
Thank you
The XML of a Component in Tridion is of the following format:
Your relevant code snippet is:
This is failing to select an element, since it is:
You seem to expect that the default namespace will be automatically selected if you don’t specify a namespace, which simply is not true. As soon as you have XML that deals with namespaces, every query will have to specify the correct namespace.
If you modify the code to deal with these two issues, it should look something like this:
You might want to consider reading up on .NET handling of namespaces in XML and on XML namespaces in general, since this is a very common mistake that you simply need to get out of your system quickly.
People who have wanted to update the Component XML through the Core Service before you found the helper class given here useful.
In addition as Mihai points out the way you invoke
XDocument.Saveis wrong. It expects a file name as its parameter, while you are passing it the XML of your Component.