在Sitecore中将类加载为列表

本文关键字:列表 类加载 Sitecore | 更新日期: 2023-09-27 18:06:58

#region Web Service GetLawyerBioInfo
        [WebMethod]
        public System.Xml.XmlNode GetLawyerBioInfo()
        {
                System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
                xDoc.PreserveWhitespace = true;
                System.Xml.XmlReaderSettings xmlRS = new System.Xml.XmlReaderSettings();
                xmlRS.IgnoreWhitespace = false;
                xmlRS.IgnoreProcessingInstructions = true;
                xmlRS.IgnoreComments = true;
                try
                {
                    LawyerInfo mInfo = GetLawyerInfo();
                    xDoc.LoadXml(LawyerInfo.GetXml(mInfo));
                    return xDoc.DocumentElement;
                }
                catch (Exception pEx)
                {
                    xDoc.LoadXml("<ErrorMessage>Error occured in GetLawyerBioInfo WS - " + pEx.Message + "</ErrorMessage>");
                    return xDoc.DocumentElement;
                }
        }
        #endregion

webservice获取xml格式的数据,这里调用GetLawyerInfo()。

        #region GetLawyerInfo
        public LawyerInfo GetLawyerInfo()
        {
           Sitecore.Data.Database mSiteCoreDB = Sitecore.Configuration.Factory.GetDatabase("master");
           LawyerInfo mInfo = new LawyerInfo();

                string query = "/sitecore/content/Global Content/People/A";
                Sitecore.Data.Items.Item root = mSiteCoreDB.GetItem(query);
                foreach (Sitecore.Data.Items.Item mBioItem in root.Children)
                {
                    mInfo.LawyerID = mBioItem.ID.ToString();
                    mInfo.LawyerName  = mBioItem.Fields["FirstName"].Value.ToString();
                    PropertyInfo[] mPropertyInfo = mInfo.GetType().GetProperties();
                    foreach (PropertyInfo mProperty in mPropertyInfo)
                    {
                        object[] mObjList = mProperty.GetCustomAttributes(false);
                        ArrayList mList = mList = new ArrayList();
                        foreach (object mObj in mObjList)
                        {
                            if (mObj is BiographyAttribute)
                            {
                                if (((BiographyAttribute)mObj).MultipleFieldsPropertyName.Equals("PracticeRelationships"))
                                {
                                    Sitecore.Data.Fields.MultilistField mMultilistField = mBioItem.Fields[((BiographyAttribute)mObj).MultipleFieldsPropertyName];
                                    foreach (Sitecore.Data.Items.Item mChild in mMultilistField.GetItems())
                                    {
                                        Sitecore.Data.Items.Item mMLI = mChild.Database.Items.GetItem(mChild.ID);
                                        PracticeItem mPitems = new PracticeItem();
                                        mPitems.PracticeTitle = mMLI.DisplayName;
                                        mPitems.PracticeID = mMLI.ID.ToString();
                                        mList.Add(mPitems);
                                    }
                                }
                            }
                        }
                        if (mProperty.Name.Equals("Practices") )
                        {
                            IBioInterface[] mItems = null;
                            if (mProperty.Name.Equals("Practices"))
                            {
                                mItems = new PracticeItem[mList.Count];
                            }
                            for (int x = 0; x < mList.Count; x++)
                            {
                                mItems[x] = (IBioInterface)mList[x];
                            }
                            mProperty.SetValue(mInfo, mItems, null);
                        }
                    }
                }
            }
         return mInfo;
        }
        #endregion

遍历A文件夹下的所有sitecore项。

        #region LawyerInfo
        [Serializable()]
        public class LawyerInfo
        {
            private string iLawyerID = "";
            private string iLawyerName = "";
            private PracticeItem[] iPractices = null;
            public LawyerInfo()
            {
            }
            #region properties
            public string LawyerID { get { return iLawyerID; } set { iLawyerID = value; } }
            public string LawyerName { get { return iLawyerName; } set { iLawyerName = value; } }
            [BiographyAttribute(HasMultipleFields = true, IsRepeatable = false, MultipleFieldsPropertyName = "PracticeRelationships")]
            public PracticeItem[] Practices { get { return iPractices; } set { iPractices = value; } }
            #endregion
            public static string GetXml(LawyerInfo pObject)
            {
                XmlSerializer mSerializer = new XmlSerializer(pObject.GetType());
                StringWriter mWriter = new StringWriter();
                mSerializer.Serialize(mWriter, pObject);
                return LawyerInfo.ReplaceXml(mWriter, false);
            }
            public static string GetXml(LawyerInfo[] pObject)
            {
                XmlSerializer mSerializer = new XmlSerializer(pObject.GetType());
                StringWriter mWriter = new StringWriter();
                mSerializer.Serialize(mWriter, pObject);
                return LawyerInfo.ReplaceXml(mWriter, true);
            }
            private static string ReplaceXml(StringWriter pWriter, bool pChangeTag)
            {
                string mString = pWriter.ToString().Replace("<?xml version='"1.0'" encoding='"utf-16'"?>", "");
                mString = mString.Replace(" xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance'" xmlns:xsd='"http://www.w3.org/2001/XMLSchema'"", "");
                mString = mString.Replace("'r'n", "");
                mString = mString.Replace("&amp;amp;", "&amp;");
                return mString;
            }
        }
        #endregion

LawyerInfo class

        #region Implementation of IBioInterface
        public class IBioInterface
        {
        }
        public class PracticeItem : IBioInterface
        {
            private string iPracticeTitle = "";
            private string iPracticeID = "";
            public PracticeItem()
            {
            }
            public string PracticeTitle { get { return iPracticeTitle; } set { iPracticeTitle = value; } }
            public string PracticeID { get { return iPracticeID; } set { iPracticeID = value; } }
        }
        #endregion

我想拥有A下的所有记录,但只在foreach循环后获得最后一条记录。如何获取子项目的所有记录

在Sitecore中将类加载为列表

你的GetLawyerInfo()方法应该是这样的:

public LawyerInfo[] GetLawyerInfo()
{
    Sitecore.Data.Database mSiteCoreDB = Sitecore.Configuration.Factory.GetDatabase("master");
    List<LawyerInfo> infos = new List<LawyerInfo>();
    string query = "/sitecore/content/Global Content/People/A";
    Sitecore.Data.Items.Item root = mSiteCoreDB.GetItem(query);
    foreach (Sitecore.Data.Items.Item mBioItem in root.Children)
    {
        LawyerInfo mInfo = new LawyerInfo();
        infos.Add(mInfo);
        mInfo.LawyerID = mBioItem.ID.ToString();
        // your code goes here
            // ...
    }
    return infos.ToArray();
}

您的解决方案遍历所有子对象,但将它们的值赋给一个LawyerInfo对象。

此解决方案将返回LawyerInfo对象的数组,并将每个子对象添加到数组中。