Updating Components using the Core Service in SDL Tridion 2011

前端 未结 3 950
别跟我提以往
别跟我提以往 2020-12-17 06:58

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          


        
3条回答
  •  北海茫月
    2020-12-17 07:21

    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:

    1. not providing a namespace to the selection
    2. only selecting direct children of the document root

    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.

提交回复
热议问题