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

LawyerInfo クラス

        #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();
}

ソリューションはすべての子を反復処理しますが、それらの値を 1 つのLawyerInfoオブジェクトに割り当てています。

このソリューションは、LawyerInfoオブジェクトの配列を返し、各子を配列に追加します。

于 2013-04-02T14:56:15.190 に答える