gpt4 book ai didi

c# - 如何通过提供 XML 源文件作为输入来创建组件

转载 作者:数据小太阳 更新时间:2023-10-29 01:59:45 26 4
gpt4 key购买 nike

我想通过在 SDL Tridion 2011 中使用核心服务 2011 直接提供 XML 源输入来创建一个组件。

我想编写代码通过上传源 XML 创建组件。使用核心服务 2011。

假设 xml 文件的名称是 helloworld.xml,位置是 D:\abcd\cdef\all\helloworld.xml

我写了这样的代码,但它不起作用。

XmlDocument contentxml = new XmlDocument();
contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml");
Response.Write("<BR>" + contentxml.ToString());
component.Content = contentxml.ToString();
ComponentData comp = (ComponentData)client.Create(component, new ReadOptions());

Response.write 没有显示任何内容。如果我遗漏了什么,请纠正我。它没有创建任何组件,但会出现错误。

当我尝试这个时:

XmlDocument contentxml = new XmlDocument();
try
{
contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml");
}
catch (XmlException exp)
{
Console.WriteLine(exp.Message);
}
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
contentxml.WriteTo(xw);
Response.Write("<BR>" + sw.ToString());

component.Content = sw.ToString();
ComponentData comp = (ComponentData)client.Create(component, new ReadOptions());

这次它显示 unable to find UUId: 类似的东西。

我的 helloworld.xml 看起来像这样。

<Content xmlns="uuid:1111eb85-0X11-11f9-1e2X-1X82X78fX920">
<first>Hello World.This is Fisrt field</first>
<second>Hello World.This is second field</second>
</Content>

如果有人分享一些示例代码来做这件事,那就太好了。

最佳答案

从文件加载任何 XML 并尝试创建组件将无法工作,除非 XML 使用 CMS 期望的格式。

SDL Tridion 中组件的 XML 结构有一些固定部分(示例节点内容、元数据)和一些灵活部分(您定义字段的方式)。首先,您需要拥有与 CMS 期望的结构相同的 XML。通常应该在您的 xml 中的节点是内容和元数据,将它们加载到 XML 文档中并使用核心服务 API 来使用这些节点中包含的内容创建一个组件。了解基于模式的组件结构的最佳方法是使用 Tridion UI 创建示例组件,并查看 XML 是如何构建的。之后,您需要使用该结构创建您的 XML 源。我最近发布了一个如何使用核心服务创建组件的示例,请看一下。

Faulted State error while creating component with Core Service

按照这段代码,您可以访问节点内容和元数据

componentData.Content = xmlUtil.GetNewXmlNode("Content", schemaData.NamespaceUri);componentData.Metadata = xmlUtil.GetNewXmlNode("元数据", schemaData.NamespaceUri);

并用您的内容替换它们

关于c# - 如何通过提供 XML 源文件作为输入来创建组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9940976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com