且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 SDL Tridion 2011 中的核心服务更新组件

更新时间:2022-04-12 21:06:28

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.