gpt4 book ai didi

c# - 在 Sitecore 中将类作为列表加载

转载 作者:数据小太阳 更新时间:2023-10-29 02:26:59 27 4
gpt4 key购买 nike

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

        #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 循环后获取最后一条记录。我怎样才能得到 child 项目的所有记录。

最佳答案

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

关于c# - 在 Sitecore 中将类作为列表加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15767155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com