无法使用SearchResultSet填充libRETS查询
本文关键字:填充 libRETS 查询 SearchResultSet | 更新日期: 2023-09-27 18:25:05
我对libRETS非常陌生。我想从MidWEST RETS服务中提取一些MLS数据。我遵循了RESO官方网站上提到的样本。http://www.reso.org/assets/Resources/CodeExamples/c-sharp-connection-ex.txt.我使用C#作为编程语言,并使用nuget包https://www.nuget.org/packages/libRETS-x64/.我可以登录,但在发送查询后,我没有得到结果。我正在下面粘贴我的样品。
using librets;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibRETSExample
{
public class RetsHelper : IDisposable
{
private List<Listing> listings = new List<Listing>();
#region Properties
/// <summary>
/// Contains the rets session connection.
/// </summary>
internal RetsSession Session
{
get
{
return _sess;
}
set
{
_sess = value;
}
}
private RetsSession _sess;
#endregion
#region Constructors
public RetsHelper(string url, string userAgent, string userName,
string password)
{
this.Session = new RetsSession(url);
//this.Session.SetUserAgent(userAgent);
this.Session.LoggerDelegate = new RetsHttpLogger.Delegate(LogRETS);
//this.Session.SetDefaultEncoding(EncodingType.RETS_XML_ISO_ENCODING);
try
{ //Log in to RETS
bool loginResult = this.Session.Login(userName, password);
if (!loginResult)
{
Trace.WriteLine("'nLogin to RETS Failed at " + DateTime.Now);
}
else
{
Trace.WriteLine("'nLogin to RETS Succeeded at " + DateTime.Now);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
#endregion
#region Instance Methods
/// <summary>
/// Gets search results for a given query.
/// </summary>
/// <param name="searchType">Resource from rets metadata</param>
/// <param name="searchClass">Class from rets metadata</param>
/// <param name="query">RETS query to run.</param>
/// <param name="offset">Record number to start at. This is 1-based, not zero-based.</param>
/// <returns>Results of query.</returns>
internal SearchResultSet GetRetsSearchResults(string searchType, string searchClass,
string query, int offset)
{
using (SearchRequest search = this.Session.CreateSearchRequest(
searchType, searchClass.ToString(), query))
{
search.SetQueryType(SearchRequest.QueryType.DMQL2);
search.SetStandardNames(false);
search.SetLimit(10);
//search.SetFormatType(SearchRequest.FormatType.COMPACT);
search.SetOffset(offset);
SearchResultSet results = this.Session.Search(search);
return results;
}
}
/// <summary>
/// Downloads all listings, starting at the given offset. This method will recurse if needed.
/// </summary>
/// <param name="propType"></param>
/// <param name="offset">Starting record of results. This is 1-based, not zero-based.</param>
internal void DownloadAllListings(string propType, int offset)
{
try
{
using (SearchResultSet results = GetRetsSearchResults("Property", propType, "(LP=300000-)", offset))
{
// get the results as a list of objects.
List<Listing> list = Populate(results);
// Add to the master list
listings.AddRange(list);
// check to see if the list finished to the end.
int totalProcessed = list.Count + offset;
if (results.GetCount() > totalProcessed)
{
// recurse if needed.
DownloadAllListings(propType, totalProcessed);
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
public List<Listing> GetAllListings(string propType, int offset)
{
listings.Clear();
DownloadAllListings(propType, offset);
return listings;
}
/// <summary>
/// Populates a ListingCollection from a RETS result set.
/// </summary>
/// <param name="results">ResultSet to create the ListingCollection from.</param>
/// <returns>ListingCollection representing the result set. Will return
/// an empty collection if no records.</returns>
private List<Listing> Populate(SearchResultSet results)
{
List<Listing> listings = new List<Listing>();
while (results.HasNext())
{
Listing currentListing = new Listing();
//sample db mapping
currentListing.StreetNumber = results.GetString("StreetNumber");
currentListing.StreetDirection = results.GetString("StreetDirection");
currentListing.StreetName = results.GetString("StreetName");
listings.Add(currentListing);
}
return listings;
}
#endregion
#region IDisposable Members
/// <summary>
/// Logout of the RETS session.
/// </summary>
public void Dispose()
{
try
{
Session.Logout();
}
finally
{
Session.Dispose();
}
}
#endregion
public void LogRETS(RetsHttpLogger.Type type, byte[] data)
{
Trace.WriteLine(type.ToString() + ": " + Encoding.UTF8.GetString(data));
}
}
public class Listing
{
public string StreetNumber { get; set; }
public string StreetDirection { get; set; }
public string StreetName { get; set; }
}
}
我面临的问题是在函数private List Populate(SearchResultSet results)内部,由于HasNext()总是返回false,代码永远不会进入while循环。但实际上查询已经发送到服务器,服务器返回结果。我能够在Trace窗口中看到它们作为xml数据。请注意,我已附上一个记录器代表。尽管服务器返回了查询结果,但我有点不知道为什么我的填充函数失败了。任何形式的帮助都将不胜感激。
您可能需要配置搜索格式类型。我知道FlexMLS需要使用COMPACT_DECODED,否则hasNext()将返回false。
searchRequest.SetFormatType(SearchRequest.FormatType.COMPACT_DECODED);