使用 SDL Tridion 2011 中的核心服务更新组件 [英] Updating Components using the Core Service in SDL Tridion 2011

查看:24
本文介绍了使用 SDL Tridion 2011 中的核心服务更新组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Tridion 2011 中的核心服务更新组件.

I am updating a Component using Core Service in Tridion 2011.

示例代码如下,

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);

我收到以下异常:

 Unable to save compSequence contains no elements 

详情:

first - 组件中字段的名称

first - name of the field in the component

有人可以帮忙解决这个问题吗?

Can any one help regarding this?

谢谢

推荐答案

Tridion 中组件的 XML 格式如下:

The XML of a Component in Tridion is of the following format:

<Content xmlns="uuid:2607D20D-1B22-4994-98C1-66D9ACF85C20">
  <first>The value of my first field</first>
  <second>The value of my second field</second>
</Content>

您的相关代码片段是:

var element = xdoc.Elements("first").Single();

这是无法选择一个元素,因为它是:

This is failing to select an element, since it is:

  1. 未为选择提供命名空间
  2. 仅选择文档根目录的直接子项

您似乎希望如果不指定命名空间,默认命名空间将被自动选择,但事实并非如此.一旦有了处理名称空间的 XML,每个查询都必须指定正确的名称空间.

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();

您可能需要考虑阅读 XML 中命名空间的 .NET 处理以及一般的 XML 命名空间,因为这是一个非常常见的错误,您只需要快速退出系统即可.

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.

在您找到给定的帮助类之前希望通过核心服务更新组件 XML 的人 此处 很有用.

People who have wanted to update the Component XML through the Core Service before you found the helper class given here useful.

此外,正如 Mihai 指出您调用 XDocument.Save 的方式是错误的.当您将组件的 XML 传递给它时,它需要一个文件名作为其参数.

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.

这篇关于使用 SDL Tridion 2011 中的核心服务更新组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆