2012-05-09 24 views
0

我在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"; 

     } 
    } 
} 
+1

你會提供你試過的代碼和你遇到的具體問題嗎? –

+0

@ Nickoli請看我編輯的代碼... 我想寫一個遞歸函數,遞歸通過模式中的每個字段,直到它達到最終的葉字段,這是沒有嵌入,以便我可以分配數據給它 – Aquarius24

回答

3

雖然我沒有你正在尋找的解決方案,我看你正在使用的核心服務,我個人更喜歡獲取Component 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 
     } 
    } 
} 
+0

謝謝johnwinter ..請看我編輯的代碼我卡住了我評論 – Aquarius24

+0

@ johnwriter你可以在下面的位置添加代碼: //檢查這個字段是否包含更多的嵌入字段 //如果它遞歸的 我無法得到。 謝謝 – Aquarius24

1

OK,我覺得有點內疚沒有幫助,但我還是我的觀點立場,這是不是一個外表套上有關的問題,你應該嘗試獲得與一般的開發實踐的一些更多的經驗。

下面是如何加載組件的內容的示例,然後將其讀遞歸使用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如何調用自己?

希望這會有所幫助。

+0

我必須創建一個新的組件,這就是爲什麼我不能使用該XML方法 – Aquarius24

+0

那麼你的問題是什麼?我有點困惑。首先你問如何循環字段,現在你問如何創建一個組件?組件內容_is_ XML,絕對沒有什麼能阻止你使用這種方法。 –

+0

@ Nuno ...好吧我會澄清你 我只有一件事與我即模架ID 我必須創建組件的基礎上,該模式ID ....並想給它的默認值 我有創建組件xml而不是從tridion獲取它 我的動機是在tridion中使用模式id創建組件,我沒有任何組件在tridion中,所以我不能讀取它的xml frm。 – Aquarius24