0

我正在使用 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);
        } 

    }
}
4

1 回答 1

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);
    }
}
于 2013-03-27T23:10:40.423 回答