我正在使用wiki api来创建一个C#程序,其中用户键入关键字并进行搜索,(如果用户搜索数据库,则XML URL将为:https://en.wikipedia.org/w/api.php?action=query&list=allcategories&acmin=10&acprefix=database&acprop=size|hidden&format=xml&aclimit=500。下拉框填充内部文本当用户从下拉框中选择不同的内部文本主题时,列表框应该填充子类别。我无法弄清楚如何填充子类别框。有谁知道我会这样做?这是我的cs代码到目前为止:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml; //needed for XML processing
using System.Net; //needed for HttpWebRequest processing
public partial class WikiExcercise : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string URL = "https://en.wikipedia.org/w/api.php?action=query&list=allcategories&acmin=10&acprefix=" + txtKeyword.Text +
"&acprop=size|hidden&format=xml&aclimit=500";
//create an xml document and locad it from the web service
XmlDocument xmlDoc = new XmlDocument();
//need to indicate a legitimate user againt (not faking from the browser)
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.UserAgent = "My application";
xmlDoc.Load(request.GetResponse().GetResponseStream());
XmlNodeList list = xmlDoc.SelectNodes("/api/query/allcategories/c[@subcats>0]");
//databind the drop down list to the XmlNodeList
ddlCategories.DataTextField = "InnerText";
ddlCategories.DataSource = list;
ddlCategories.DataBind();
}
protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
string URL = "https://en.wikipedia.org/w/api.php?action=query&list=allcategories&acmin=10&acprefix=" + txtKeyword.Text +
"&acprop=size|hidden&format=xml&aclimit=500";
//create an xml document and locad it from the web service
XmlDocument xmlDoc = new XmlDocument();
//need to indicate a legitimate user againt (not faking from the browser)
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.UserAgent = "My application";
xmlDoc.Load(request.GetResponse().GetResponseStream());
XmlNodeList Xn = xmlDoc.SelectNodes("/api/query/allcategories/c[@subcats>0]/@subcats");
lstSubCategories.DataTextField = "InnerText";
lstSubCategories.DataSource = Xn;
lstSubCategories.DataBind();
foreach (XmlNode xNode in Xn)
{
lstSubCategories.Items.Add("boo");
lstSubCategories.DataTextField = "InnerText";
//lstSubCategories.Items.Add(xNode.Attributes["subcats"].Value);
}
}
}
答案 0 :(得分:0)
我这是Windows窗体应用程序的一部分,而不是ASP.NET页面,这对我的设备来说速度更快。代码不应该太大不同。
我必须调整子类别查询以匹配https://www.mediawiki.org/wiki/API:Categorymembers的规范和XPath语句。
此代码适用于显示子类别的列表框。你可以做很多事情,比如在自定义类中保存类别/页面信息,这样你就可以通过他们的ID来引用wiki页面,但我没有那么做。您可以在.Items.Add(...)调用中实例化这样的对象。
protected void btnSubmit_Click(object sender, EventArgs e)
{
string URL = "https://en.wikipedia.org/w/api.php?action=query&list=allcategories&acmin=10&acprefix=" + txtKeyword.Text +
"&acprop=size|hidden&format=xml&aclimit=500&cmtype=subcat";
//create an xml document and locad it from the web service
XmlDocument xmlDoc = new XmlDocument();
//need to indicate a legitimate user againt (not faking from the browser)
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.UserAgent = "My application";
xmlDoc.Load(request.GetResponse().GetResponseStream());
XmlNodeList list = xmlDoc.SelectNodes("/api/query/allcategories/c[@subcats>0]");
ddlCategories.Items.Clear();
foreach(XmlNode n in list)
{
ddlCategories.Items.Add(n.InnerText);
}
}
protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
string URL = "https://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:" + ddlCategories.SelectedItem + "&format=xml&cmlimit=500";
//create an xml document and locad it from the web service
XmlDocument xmlDoc = new XmlDocument();
//need to indicate a legitimate user againt (not faking from the browser)
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.UserAgent = "My application";
xmlDoc.Load(request.GetResponse().GetResponseStream());
XmlNodeList Xn = xmlDoc.SelectNodes("/api/query/categorymembers/cm/@title");
lstSubCategories.Items.Clear();
foreach(XmlNode n in Xn)
{
lstSubCategories.Items.Add(n.InnerText);
}
}