2009-11-29 39 views
1

试图建构一个辅助类将返回一个ArrayList,但我发现了以下错误,具有xml文档我需要创建办:以为我明白静态类

Util.oDocument “:不能在静态类

我想我明白你为什么会不希望创建一个新的xmlDoc中对象的每个调用此方法时,可以声明实例成员,但我需要一个文档中出现的功能。我应该如何处理这个问题?

using System; 
using System.Collections; 
using System.Xml; 

public static class Util 
{ 

    public static ArrayList multipleArtistList(string artistName) 
    { 
     XmlDocument oDocument = new XmlDocument(); 

     string uri = "http://api.leoslyrics.com/api_search.php?auth=duane&artist=" + artistName; 
     oDocument.Load(uri); 

     XmlNodeList results = oDocument.GetElementsByTagName("name"); 
     ArrayList artistList = new ArrayList(); 

     for (int i = 0; i < results.Count; i++) 
     { 
      if (!artistList.Contains(results[i].InnerText)) 
      { 
       artistList.Add(results[i].InnerText); 

      } 

     } 

     return artistList; 
    } 

} 
+1

你/确认/那oDocument在方法内部声明,或者是一个领域? – 2009-11-29 05:49:46

+2

我看不出那个代码有什么问题。如果oDocument的声明超出了任何函数,您应该只会得到该错误。 – Foole 2009-11-29 05:49:58

+0

以前的评论似乎是正确的。否则,您将无法声明您正在使用的任何其他变量。 – 2009-11-29 05:55:21

回答

4

在这里,这错误:

Util.oDocument: cannot declare instance members in a static class 

意味着你已经声明oDocument 外的方法

您发布的代码没有问题,实际上错误和代码相互矛盾。

确保在方法中声明了oDocument。如果你想将其声明为一个字段中,确保给它static修改,就像这样:

public static class Util 
{ 
    static XmlDocument oDocument; 

    /* code */ 
} 
+0

很酷,谢谢。试了一遍,它很好。很高兴知道我在正确的轨道上。 – fieldingmellish 2009-11-29 05:58:33

+3

也,谢谢你使用你的内部声音。 – fieldingmellish 2009-11-29 05:59:09