2013-04-02 19 views
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()。在Sitecore中加載類列表

 #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類

 #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 

我想有一個下的所有記錄,但只有foreach循環後獲得最後一個記錄。我如何獲得兒童項目的所有記錄。

回答

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對象數組,並將每個子對象添加到數組中。

+0

Thanks @Maras但GetLawyerInfo()來自LawyerInfo mInfo = GetLawyerInfo(); xDoc.LoadXml(LawyerInfo.GetXml(mInfo));所以在轉換爲列表後,如何從中獲取XML – Sam

+0

Hi Sam。我已經更改了代碼,所以它返回數組'LawyerInfo []',並且在LawyerInfo類中還有另一種序列化數組的方法('public static string GetXml(LawyerInfo [] pObject)')。 –

+0

感謝瑪拉斯工作的魅力....我在這個小問題上如此困惑,但現在明確..再次感謝 – Sam