2013-03-27 73 views
0

我使用的是維基API來創建一個C#程序,其中一個關鍵字和搜索用戶類型(如果用戶搜索數據庫中的XML網址是:。https://en.wikipedia.org/w/api.php?action=query&list=allcategories&acmin=10&acprefix=database&acprop=size|hidden&format=xml&aclimit=500一個下拉框與內部文本當用戶選擇填充。?從下拉框中選擇不同的內部文本對象,列表框中應填寫了子類我無法弄清楚如何填補的子類別框有誰知道whow我會做到這一點這是CS的代碼,我到目前爲止有:如何使用Xml REST查詢中的Xml屬性填充ListBox?

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

我這樣做是爲Windows窗體應用程序,而不是一個ASP.NET頁,它只是更快我的設備。該代碼不應該過多不同的一部分。

我必須調整您的子類別查詢以匹配https://www.mediawiki.org/wiki/API:Categorymembers和您的XPath語句的規格。

此代碼對我的作品,以顯示子類別列表框。你可以做很多事情,比如在自定義類中保存類別/頁面信息,這樣你就可以通過ID來引用維基頁面,但是我沒有那麼做。你會在.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); 
    } 
}