How to populate a ListBox with Xml attributes from an Xml REST query?

Hard2Beat

New Member
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:\[code\] 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 processingusing System.Net; //needed for HttpWebRequest processingpublic 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); } }}\[/code\]
 
Back
Top