📄 liveexpocategories.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.IO;
using System.Net;
using LiveExpoService.LiveExpo;
namespace LiveExpoService
{
/// <summary>
/// LiveExpoCategories contains functionality to retrieve a list of categories for which items can be searched
/// on the Windows Live Expo Service. All categories available are maintained in a generics List object for later
/// use to actually search for specific items on the Expo Service.
/// </summary>
public class LiveExpoCategories
{
private static LiveExpoCategories theInstance;
private static List<LiveExpoCategory> categories;
private LiveExpoCategories()
{
}
/// <summary>
/// Instance
///
/// LiveExpoCategories is ipmlemented as a singleton, meaning there is at most one instance of the class available at any time.
/// This method creates the instance or simply returns it when it was created earlier.
/// </summary>
/// <returns>LiveExpoCategories - The one and only instance of this class.</returns>
public static LiveExpoCategories Instance()
{
if (theInstance == null)
{
theInstance = new LiveExpoCategories();
}
return theInstance;
}
/// <summary>
/// NrCategoriesAvailable
///
/// Return the number of categories that are currently available for the Windows Live Expo Service.
/// </summary>
/// <returns>int - Number of categories for Live Expo.</returns>
public int NrCategoriesAvailable()
{
return categories == null ? 0 : categories.Count;
}
/// <summary>
/// Indexer to retrieve a particular category.
/// </summary>
/// <param name="index">int - value between 0 and NrCategoriesAvailable.</param>
/// <returns>LiveExpoCategory - The specified category.</returns>
public LiveExpoCategory this[int index] // Indexer declaration
{
get
{
// Check the index limits.
if (index < 0 || index >= categories.Count)
{
return null;
}
else
{
return categories[index];
}
}
}
/// <summary>
/// GetCategories
///
/// Retrieve all available categories from the Windows Live Expo Service.
/// </summary>
/// <returns>List - Generics list of LiveExpoCategory objects</returns>
public List<LiveExpoCategory> GetCategories()
{
return GetCategories(false, false);
}
/// <summary>
/// GetCategories
///
/// Retrieve all available categories from the Windows Live Expo Service.
/// </summary>
/// <param name="useXMLReader">bool - True if using XmlReader to retrieve information, False if using XmlDocument.</param>
/// <returns>List - Generics list of LiveExpoCategory objects</returns>
public List<LiveExpoCategory> GetCategories(bool useXMLReader)
{
return GetCategories(false, useXMLReader);
}
/// <summary>
/// GetCategories
///
/// Retrieve all available categories from the Windows Live Expo Service. The returned XML string contains categories
/// and subcategories within those categories.
/// </summary>
/// <param name="refresh">bool - True to refresh the category list, false to return an existing list.</param>
/// <param name="useXMLReader">bool - True if using XmlReader to retrieve information, False if using XmlDocument.</param>
/// <returns>List - Generics list of LiveExpoCategory objects</returns>
/// <remarks>
/// We distinguish between XmlReader and XmlDocument for possible performance reasons (XmlReader is supposed to be faster).
/// </remarks>
public List<LiveExpoCategory> GetCategories(bool refresh, bool useXMLReader)
{
if (categories == null || refresh)
{
categories = new List<LiveExpoCategory>();
}
if (useXMLReader)
{
GetCategoriesWithXmlReader();
}
else
{
GetCategoriesWithXmlDoc();
}
return categories;
}
/// <summary>
/// GetCategoriesDS
///
/// Retrieve all Windows Live Expo Service categories in a dataset for further processing / display purposes.
/// </summary>
/// <returns>DataSet containing the categories of the Windows Live Expo Service.</returns>
public DataSet GetCategoriesDS()
{
DataSet ds = new DataSet();
try
{
Classifieds_Listings cl = new Classifieds_Listings();
string rssXmlData = cl.GetCategories(0, LiveExpoHelper.Credentials);
XmlTextReader xmlRdr = new XmlTextReader(new StringReader(rssXmlData));
ds.ReadXml(xmlRdr);
}
catch (WebException we)
{
System.Windows.Forms.MessageBox.Show(we.Message, "Error calling Live Expo");
ds = null;
}
catch (XmlException xe)
{
System.Windows.Forms.MessageBox.Show(xe.Message, "Error converting to XML");
ds = null;
}
return ds;
}
/// <summary>
/// GetCategoriesWithXmlDoc
///
/// Retrieve all available categories from the Windows Live Expo Service. The returned XML string contains categories
/// and subcategories within those categories. To distinguish between them we loop through all ChildNodes of the FirstChild, but
/// also through all inner nodes of those child nodes.
/// </summary>
/// <remarks>
/// The Windows Live Expo Service returns XML data that we load into a XmlDocument for further processing.
/// </remarks>
private void GetCategoriesWithXmlDoc()
{
XmlDocument liveExpoCategoriesDoc;
try
{
// Request the information from the Windows Live Expo Service.
Classifieds_Listings cl = new Classifieds_Listings();
string response = cl.GetCategories(0, LiveExpoHelper.Credentials);
liveExpoCategoriesDoc = new XmlDocument();
liveExpoCategoriesDoc.LoadXml(response);
}
catch (WebException we)
{
System.Windows.Forms.MessageBox.Show(we.Message, "Error calling Live Expo");
return;
}
foreach (XmlNode entry in liveExpoCategoriesDoc.FirstChild.ChildNodes)
{
RetrieveExpoCategory(entry, false);
foreach (XmlNode entryInner in entry.ChildNodes)
{
RetrieveExpoCategory(entryInner, true);
}
}
}
/// <summary>
/// RetrieveExpoCategory
///
/// Retrieve all the information belonging to a found category of the Windows Live Expo Service.
/// </summary>
/// <param name="currentNode">XmlNode - Current node in the XML response of the Live Expo Service.</param>
/// <param name="subCategory">bool - True if this is a sub category, else false.</param>
/// <returns>bool - True if a valid category is found, else false.</returns>
private bool RetrieveExpoCategory(XmlNode currentNode, bool subCategory)
{
int id = 0;
bool categoryFound = false;
foreach (XmlAttribute a in currentNode.Attributes)
{
if (a.LocalName.CompareTo("id") == 0)
{
id = Convert.ToInt32(a.Value);
}
if (a.LocalName.CompareTo("name") == 0)
{
string name = a.Value;
if (subCategory)
name = " " + a.Value;
categories.Add(new LiveExpoCategory(id, name));
categoryFound = true;
}
}
return categoryFound;
}
/// <summary>
/// GetCategoriesWithXmlReader
///
/// Retrieve all available categories from the Windows Live Expo Service. The returned XML string contains categories
/// and subcategories within those categories. We make no distinction between categories and subcategories, we simply read
/// attributes from all nodes with the name "classifieds:category".
/// </summary>
/// <remarks>
/// The Windows Live Expo Service returns XML data that we load into an XmlTextReader for further processing.
/// </remarks>
private void GetCategoriesWithXmlReader()
{
XmlTextReader xmlRdr;
try
{
Classifieds_Listings cl = new Classifieds_Listings();
string rssXmlData = cl.GetCategories(0, LiveExpoHelper.Credentials);
xmlRdr = new XmlTextReader(new StringReader(rssXmlData));
}
catch (WebException we)
{
System.Windows.Forms.MessageBox.Show(we.Message, "Error calling Live Expo");
return;
}
while (xmlRdr.ReadToFollowing("classifieds:category"))
{
xmlRdr.MoveToAttribute("classifieds:id");
xmlRdr.ReadAttributeValue();
int id = Convert.ToInt32(xmlRdr.Value);
xmlRdr.MoveToAttribute("classifieds:name");
xmlRdr.ReadAttributeValue();
string name = xmlRdr.Value;
categories.Add(new LiveExpoCategory(id, name));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -