3
#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 文件夹下的所有站点核心项目。

        #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

律师信息类

        #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 循环之后获取最后一条记录。如何获取儿童项目的所有记录。

4

1 回答 1

4

您的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对象数组并将每个子对象添加到数组中。

于 2013-04-02T14:56:15.190 回答