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
The XML of a Component in Tridion is of the following format:
The value of my first field
The value of my second field
Your relevant code snippet is:
var element = xdoc.Elements("first").Single();
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:
XNamespace ns = xdoc.Root.GetDefaultNamespace();
var element = xdoc.Descendants(ns+"first").Single();
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.Save
is wrong. It expects a file name as its parameter, while you are passing it the XML of your Component.