2014-07-07 24 views
-4

我創建了一個類庫,現在我想在網站上使用它。在我的網站上使用類庫C#

using System; 
using System.Web.UI; 

namespace FeedPath 
{ 
    public partial class ClassFeed : System.Web.UI.Page 
    { 
     public void ParseFeed(string builtUrl, int maxFeed, string code) 
     { 
      System.Net.WebRequest myRequest = System.Net.WebRequest.Create(builtUrl); 
      System.Net.WebResponse myResponse = myRequest.GetResponse(); 
      System.IO.Stream rstream = myResponse.GetResponseStream(); 
      System.Xml.XmlDocument rdoc = new System.Xml.XmlDocument(); 
      rdoc.Load(rstream); 
      System.Xml.XmlNodeList ritems = rdoc.SelectNodes("rss/channel/item"); 
      string title = "", link = "", sdescription = ""; 
      string staticstring = code; 
      for (int i = 0; i < maxFeed; i++) 
      { 
       staticstring = staticstring + i; 
       System.Xml.XmlNode rdetail; 
       title = ""; 
       link = ""; 
       sdescription = ""; 
       rdetail = ritems.Item(i).SelectSingleNode("title"); 
       if (rdetail != null) 
       { 
        title = rdetail.InnerText; 
       } 
       rdetail = ritems.Item(i).SelectSingleNode("link"); 
       if (rdetail != null) 
       { 
        link = rdetail.InnerText; 
       } 
       rdetail = ritems.Item(i).SelectSingleNode("description"); 
       if (rdetail != null) 
       { 
        sdescription = rdetail.InnerText; 
       } 
       Response.Write("<li id='" + staticstring + "'><h3><a href='" + link + "' target='new'>" + title + "</a></h3>" + sdescription + "</li>"); 
       staticstring = code; 
      } 
     } 

    } 
} 

然後在我的.aspx使用

<% string fd01Url = "https://bitly.com/u/l3ny.rss"; ParseFeed(fd01Url, 7, "bl"); %> 

我加了參考,但現在我無法對其進行初始化。

我想:

using FeedPath; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     FeedPath.ClassFeed Myopject = new.FeedPath(); 

    } 
} 

,但沒有運氣。

下面的圖像是我提出的類:

enter image description here

+1

歡迎來到Stack Overflow。有幾件事情可以確保你提出很好的問題:在問題本身中包含實際的代碼。這意味着作爲一種資源來幫助每個人,而不僅僅是你。您的圖片可能無法提供給此問題的未來觀衆。您還需要顯示所有相關代碼,而不僅僅是一小段代碼。請編輯您的問題以提供此信息。 – mason

+0

您的「類庫」包含一個從Page繼承的類。爲什麼? – sh1rts

+0

我想我沒有把所有的代碼。我的錯。 @ sh1rts我是新手我不知道,但我想因爲我使用<%string fd01Url =「https://bitly.com/u/l3ny.rss」; ParseFeed(fd01Url,7,「bl」); %>在abc.aspx頁面上?不知道,謝謝。 – l3ny

回答

1

你的語法都錯了,這就是原因。

protected void Page_Load(object sender, EventArgs e) 
{ 
    FeedPath.ClassFeed Myobject = new FeedPath.ClassFeed(); 
    // use `Myobject` to call instance methods. For example: 
    Myobject.ParseFeed(...); 
} 

這也是值得注意的是,因爲你已經添加using FeedPath;,您可以撥打ClassFeed不使用完全合格的命名空間。因此,這也將工作:

using FeedPath; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    ClassFeed Myobject = new ClassFeed(); 
} 

我建議你去通過一些C#教程/演練,並繼續對你的項目之前,適當學習語言的基礎知識。

+0

每個教程它的指涉,我想如果你不願意幫助你不應該發表意見。無論如何,謝謝你。 – l3ny

+0

@ l3ny是的,每個教程都是不同的,但他們仍然會教你大部分的語言基礎知識。我怎麼不願意幫忙?我的答案包含解決您的問題。閱讀教程僅僅是一個建議,你可以決定是否遵循。 –

+0

是的你是對的,我只是覺得,因爲我正在學習研究,我想應該感覺更少。但是,是的,你有正確的解決方案,謝謝。 – l3ny

-1

好的。我會在這一個拍攝。

您需要能夠實例化您的FeedPath類。這是通過new命令完成的。 然後調用你的方法與你的變量在這種情況下myObject.ParseFeed()

using FeedPath; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     FeedPath myObject = new FeedPath(); 
     myobject.ParseFeed() //Fill the() of your .ParseFeed() method with your parameters.  
    } 
}