2012-09-29 167 views
1

我于2009年特里东工作使用.NET模板C#2.0阅读从文件夹的子文件夹和所有组件

我需要从文件夹和子文件夹的读取所有组件。

如果在我的代码我写:

OrganizationalItem imageFolder = 
    (OrganizationalItem)m_Engine.GetObject(comp.OrganizationalItem.Id); 

我能够从那里指示组件存在的地方读取所有组件中的子文件夹,但我无法读取其他组件存在于存在指示符的文件夹。

但如果我写

OrganizationalItem imageFolder = (OrganizationalItem)m_Engine.GetObject(
           comp.OrganizationalItem.OrganizationalItem.Id); 

然后我能够只读文件夹,指标成分的存在。

以下是我的代码。

XmlDocument doc = xBase.createNewXmlDocRoot("ImageLibrary"); 
XmlElement root = doc.DocumentElement; 


Filter filter = new Filter(); 
Component comp = this.GetComponent(); 

filter.Conditions["ItemType"] = ItemType.Folder; 
filter.Conditions["Recursive"] = "true"; 

OrganizationalItem imageFolder = 
     (OrganizationalItem)m_Engine.GetObject(comp.OrganizationalItem.Id);    
XmlElement itemList = imageFolder.GetListItems(filter); 

foreach (XmlElement itemImg in itemList) 
{ 
    filter.Conditions["ItemType"] = ItemType.Component; 
    filter.Conditions["BasedOnSchema"] = comp.Schema.Id; 

    OrganizationalItem imgFolder = 
     (OrganizationalItem)m_Engine.GetObject(itemImg.GetAttribute("ID") 
     .ToString()); 
    XmlElement imageLibs = imgFolder.GetListItems(filter); 

    doc = this.createImageNodes(imageLibs, doc, filter, comp); 
    foreach (XmlElement imglib in imageLibsList) 
    { 
     XmlElement imageroot = doc.CreateElement("Image"); 
     XmlElement uploadeddateNode = doc.CreateElement("DateUploaded"); 
     Component imgComp =  
       (Component)m_Engine.GetObject(imglib.GetAttribute("ID")); 
    } 
} 

请建议。

回答

6

我看到你的片段很多多余的代码有关的问题“读从文件夹和子文件夹中的所有组件”

但是回答这个问题本身,当你正在做的:

OrganizationalItem imageFolder = (OrganizationalItem)m_Engine.GetObject(comp.OrganizationalItem.Id); 

你是无法读取该文件夹中存在的组件,因为您之前只在以下行上将筛选器设置为文件夹:

filter.Conditions["ItemType"] = ItemType.Folder; 

解决方案:

如果你想要检索的“指示器组件”文件夹中的所有组件及以下,你需要设置你的第一个搜索过滤器如下:

filter.Conditions["Recursive"] = "true"; 
filter.Conditions["ItemType"] = ItemType.Component; 
filter.Conditions["BasedOnSchema"] = comp.Schema.Id; 

并执行搜索:

OrganizationalItem imageFolder = (OrganizationalItem)m_Engine.GetObject(comp.OrganizationalItem.Id);    
XmlElement itemList = imageFolder.GetListItems(filter); 
+0

非常感谢。今天,我没有时间来解决这个问题。我会测试这个并回到你身边。再次感谢 – user1678234

0

我想你会想要收集子文件夹和递归调用你的每个人的功能,这似乎是你想要实现的。

这个函数叫做createImageNodes(),你在哪里设置imageLibsList?

它看起来像你在第一个循环中将每个项目视为一个文件夹,那么组件呢?

+0

是的,我需要阅读我的指示器组件存在的所有组件。另外,请阅读文件夹中的所有组件以及存在指示符的子文件夹。 – user1678234

2

很基本的东西。尽量避免使用Filter类,因为它在2009年已被弃用,并且尽可能多地使用GetListItems作为获取列表的速度总是更快。

public class GetComponentsInSameFolder : ITemplate 
{ 
    public void Transform(Engine engine, Package package) 
    { 
     TemplatingLogger log = TemplatingLogger.GetLogger(GetType()); 
     if (package.GetByName(Package.ComponentName) == null) 
     { 
      log.Info("This template should only be used with Component Templates. Could not find component in package, exiting"); 
      return; 
     } 
     var c = (Component)engine.GetObject(package.GetByName(Package.ComponentName)); 
     var container = (Folder)c.OrganizationalItem; 
     var filter = new OrganizationalItemItemsFilter(engine.GetSession()) { ItemTypes = new[] { ItemType.Component } }; 

     // Always faster to use GetListItems if we only need limited elements 
     foreach (XmlNode node in container.GetListItems(filter)) 
     { 
      string componentId = node.Attributes["ID"].Value; 
      string componentTitle = node.Attributes["Title"].Value; 
     } 

     // If we need more info, use GetItems instead 
     foreach (Component component in container.GetItems(filter)) 
     { 
      // If your filter is messed up, GetItems will return objects that may 
      // not be a Component, in which case the code will blow up with an 
      // InvalidCastException. Be careful with filter.ItemTypes[] 
      Schema componentSchema = component.Schema; 
      SchemaPurpose purpose = componentSchema.Purpose; 
      XmlElement content = component.Content; 
     } 
    } 
} 
相关问题