2012-04-09 48 views
2

我更新在外表套上使用核心服务2011.更新组件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 - 的名字组件中的字段

任何人都可以帮忙吗?

谢谢

回答

12

在外表套上组件的XML是格式如下:

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

你的相关代码段是:

​​

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

  1. 没有提供一个命名空间来选择
  2. 只选择文档根的直接子

你似乎期望默认命名空间会,如果你不指定命名空间自动选择,这根本不是真的。只要有XML处理名称空间,每个查询都必须指定正确的名称空间。

如果修改代码来处理这两个问题,就应该是这个样子:

XNamespace ns = xdoc.Root.GetDefaultNamespace(); 
var element = xdoc.Descendants(ns+"first").Single(); 

你可能要考虑在XML和对XML命名空间的命名空间的.NET处理读了一般,因为这是一个非常常见的错误,你只需要快速离开你的系统。

在您发现给定here的助手类有用之前,想要通过Core Service更新Component XML的人。

此外,由于米海指出你调用XDocument.Save的方式是错误的。它期望一个文件名作为它的参数,而你将它传递给你的组件的XML。

+0

谢谢。从答案中得到了很多信息。 – Patan 2012-04-09 16:23:26

3

您的代码尝试将XML DOM保存到文件中。

XDocument.Save(string fileName) - 将此XDocument序列化为文件,覆盖现有文件(如果存在)。

你应该使用这样的事情:

using (var client = new SessionAwareCoreServiceClient(netTcpBinding, remoteAddress)) 
{ 
    ReadOptions readOptions = new ReadOptions(); 
    ComponentData component = client.Read(compTcmUri, readOptions) as ComponentData; 
    XDocument dom = XDocument.Parse(component.Content); 
    // do your modifications to dom 
    component.Content = dom.ToString(); 
    component = client.Update(component, readOptions) as ComponentData; 

    Console.WriteLine("Component updated: " + component.LocationInfo.WebDavUrl); 
} 
+0

我已经试过以上代码,但我无法更新组件。 – Patan 2012-04-09 13:08:29

+0

小心提供更多信息? – 2012-04-09 15:52:25

2

有时,特别是如果您正在同步来自作为主服务器的不同存储库的内容,则可以从零开始重新构建组件。 在这种情况下,这里是如何做一个基本的例子:

public static void updateComponent() 
    { 
     string componentWdUrl = "/webdav/020%20Content/Building%20Blocks/Content/wstest/testComponent.xml"; 
     CoreServicesUtil coreServicesUtil = new CoreServicesUtil(); 
     coreServicesUtil.coreServiceClient.CheckOut(componentWdUrl, true, coreServicesUtil.readOptions); 
     ComponentData componentData = coreServicesUtil.getComponentData(componentWdUrl); 
     SchemaData schemaData = coreServicesUtil.getSchemaData(componentData.Schema.IdRef); 
     componentData.Content = xmlUtil.GetNewXmlNode("Content", schemaData.NamespaceUri); 
     componentData.Metadata = xmlUtil.GetNewXmlNode("Metadata", schemaData.NamespaceUri); 
     componentData.AddSingleField("singlefield", "singlefield sample", schemaData.NamespaceUri); 
     componentData = (ComponentData)coreServicesUtil.coreServiceClient.Save(componentData, coreServicesUtil.readOptions); 
     coreServicesUtil.coreServiceClient.CheckIn(componentData.Id, coreServicesUtil.readOptions); 
     coreServicesUtil.coreServiceClient.Close(); 
    } 

有关此示例中使用的方法的更多描述的文章中解释说:

Faulted State error while creating component with Core Service