递归函数,遍历模式中的每个嵌入式模式字段以到达叶数据字段

时间:2012-05-09 04:03:05

标签: tridion

我在Tridion中有一个模式,它有嵌入的模式字段,可能还有嵌入字段。

我想要到达最终的叶子字段,以便我可以为它分配一些值。为此,我想编写递归函数,循环遍历每个字段,直到它到达最终字段。

我正在使用SDL Tridion 2011中的核心服务实现

我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using System.Net;
using System.Xml;
using Tridion.ContentManager.CoreService.Client;
using System.Text;
using Tridion.ContentManager.CoreService;
using System.ServiceModel.Channels;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Data;
using System.Configuration;


namespace Loading_Utility
{
    public partial class TST : System.Web.UI.Page
    {
        Fields obj = new Fields();
        protected void Page_Load(object sender, EventArgs e)
        {
            using (ChannelFactory<ISessionAwareCoreService> factory =
            new ChannelFactory<ISessionAwareCoreService>("wsHttp_2011"))
            {
                ISessionAwareCoreService client = factory.CreateChannel();
                var schemaFields = client.ReadSchemaFields("tcm:202-2242-8", true, new ReadOptions());
                ComponentData component = (ComponentData)client.GetDefaultData(ItemType.Component, "tcm:202-638-2");
                var fields = Fields.ForContentOf(schemaFields);
                component.Schema.IdRef="tcm:202-2242-8";
            }
        }
        public void fieldRecursion(Field field)
        {
            //var getFields = fields;
            if (field.GetType() == typeof(EmbeddedSchemaFieldDefinitionData))
            {
                // code for checking further if field is embedded or not


                //Field newField = field.GetSubFields().GetFieldElements( new ItemFieldDefinitionData() as Field)
                //fieldRecursion(recursiveField);
            }
            //string fieldName = recursiveField.Name;
            //fields[fieldName] = "HI";

        }
    }
}

2 个答案:

答案 0 :(得分:3)

虽然我没有您正在寻找的解决方案,但我发现您正在使用核心服务,我个人更喜欢获取组件XML(Component.Content)并根据需要对其进行解析/操作。也许如果您可以在此处粘贴XML,我可以将其放入我的一个示例核心服务项目中并向您发送解决方案吗?

如果这对你没有帮助,我已经看过了api,这应该可以帮助你走上正确的道路。也许一旦你有解决方案,你可以把它贴在这里?

public void RecurseEmbeddedFields(SchemaFieldsData schemaFields)
{
    foreach (ItemFieldDefinitionData field in schemaFields.Fields)
    {
        if (field.GetType() == typeof(EmbeddedSchemaFieldDefinitionData))
        {
            // check if this field contains more embedded fields
            // if it does recurse
        }
    }
}

答案 1 :(得分:1)

好吧,我对没有帮助感到有些内疚,但我仍然认为这不是与Tridion相关的问题,你应该尝试获得更多的一般开发实践经验。

以下是如何加载Component的内容,然后使用Xml以递归方式读取它的示例:

组件的Xml:

<Content xmlns="uuid:02395f72-acef-44e8-9c35-ff8c9f380251">
    <EmbeddedSchema1>
        <SomeField>Hello</SomeField>
        <EmbeddedSchema2>
            <ATextField>There</ATextField>
        </EmbeddedSchema2>
    </EmbeddedSchema1>
</Content>

核心服务代码:

static void Main(string[] args)
{
    SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("wsHttp_2011");
    ReadOptions readOptions = new ReadOptions();

    ComponentData component = (ComponentData)client.Read("tcm:5-3234", readOptions);
    Console.WriteLine("Find fields recursively");

    XmlDocument content = new XmlDocument();
    content.LoadXml(component.Content);
    SchemaData schema = (SchemaData)client.Read(component.Schema.IdRef, readOptions);
    XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
    ns.AddNamespace("content", schema.NamespaceUri);

    foreach (XmlElement node in content.SelectNodes("content:*", ns))
    {
        ReadContentRecursively(node, ns);
    }
    client.Close();
}
private static void ReadContentRecursively(XmlElement node, XmlNamespaceManager ns)
{
    if(!string.IsNullOrEmpty(node.InnerText))
    {
        foreach (XmlNode innerNode in node)
        {
            if(innerNode is XmlText)
            {
                Console.WriteLine("Node " + node.Name + " with value \"" + innerNode.Value + "\"");
            }
        }
    }
    if(node.SelectNodes("content:*", ns).Count > 0)
    {
        foreach (XmlElement childNode in node.SelectNodes("content:*", ns))
        {
            Console.WriteLine("Found Field: " + childNode.Name);
            ReadContentRecursively(childNode, ns);
        }
    }
}

注意ReadContentRecursively如何调用自身?

希望这有帮助。