I am using a wiki api to create a C# program where a user types in a keyword and searches, (if the user searches database the XML url would be: https://en.wikipedia.org/w/api.php?action=query&list=allcategories&acmin=10&acprefix=database&acprop=size|hidden&format=xml&aclimit=500 . A dropdown box populates with the Inner Text. When a user selects different Inner Text subjects from the dropdown box, the listbox should fill up with the subcategories. I cant figure out how to fill the subcategories box. Does anyone know whow I would do this? This is the cs code I have so far:
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);
}
}
}
I did this as part of a windows forms app instead of an ASP.NET page, which was just faster for my devices. The code shouldn't be too much different.
I had to adjust your subcategory query to match the specs at https://www.mediawiki.org/wiki/API:Categorymembers and your XPath statement.
This code works for me to display a list box of sub categories. There are plenty of things you can do, like save category/page information in a custom class so you can reference wiki pages by their IDs, but I didn't do that stuff. You would instantiate such an object in the .Items.Add(...) calls.
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.