2015-04-28 21 views
0

我有我的主要程序類的列表獲取值調用StronyElementuStrukt程序C#不能從對象

List<object> monthlyPages = new List<object>(); 
monthlyPages = StronyElementuStrukt(loginGuid, "8B35134E10A8432DB1A8C06A58427988"); 

下面是該過程 - 即建立XML節點列表,並返回到主程序的方法類:

public static List<object> StronyElementuStrukt(string LoginGUID, string LinkGUID) 
{ 
    List<object> listPages = new List<object>(); 

    XmlDocument document = new XmlDocument(); // tworzenie nowego obiektu - dokument xml z odpowiedzia serwera 
    document.LoadXml(response.Result); //wczytywanie xmla z odpowiedzia serwera do obiektu 
    XmlNode pageNode = document.SelectSingleNode("/IODATA/PAGES/PAGE"); //deklaracja noda xmlowego 

    if (pageNode != null) //jeżeli PAGE node istnieje 
    { 
     XmlNodeList nodeList = document.SelectNodes("//PAGE"); 

     foreach (XmlNode node in nodeList) 
     { 
      listPages.Add(node); 
     } 
     return listPages; 
    } 
} 

在主程序類我需要拿起XML id屬性的值,我試圖做這樣的:

foreach (object monthlyPage in monthlyPages) 
{ 
    Console.WriteLine(monthlyPage.Attributes["id"].Value); 
} 

的問題是,當我試圖讓id我得到以下錯誤:

Error 6 'object' does not contain a definition for 'Attributes' and no extension method 'Attributes' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

你能告訴我如何到達XML在foreach循環屬性,好嗎?請問是否有不清楚的地方。

+0

爲什麼不使用'名單'?此外,在重新分配「StronyElmentStrukt」的結果之前,不需要爲「monthlyPages」分配新的空列表。 – juharr

+0

輝煌,它的工作原理,我把它從'List '改成'List '。非常感謝 :) – crazylane

回答

3

更改方法返回List<XmlNode>

public static List<XmlNode> StronyElementuStrukt(string LoginGUID, string LinkGUID) 
{ 
    List<XmlNode> listPages = new List<object>(); 

    XmlDocument document = new XmlDocument(); // tworzenie nowego obiektu - dokument xml z odpowiedzia serwera 
    document.LoadXml(response.Result); //wczytywanie xmla z odpowiedzia serwera do obiektu 
    XmlNode pageNode = document.SelectSingleNode("/IODATA/PAGES/PAGE"); //deklaracja noda xmlowego 

    if (pageNode != null) //jeżeli PAGE node istnieje 
    { 
     XmlNodeList nodeList = document.SelectNodes("//PAGE"); 

     foreach (XmlNode node in nodeList) 
     { 
      listPages.Add(node); 
     } 
    } 

    return listPages; 
} 

然後這將工作。

List<XmlNode> monthlyPages = StronyElementuStrukt(
    loginGuid, 
    "8B35134E10A8432DB1A8C06A58427988"); 

foreach (XmlNode monthlyPage in monthlyPages) 
{ 
    Console.WriteLine(monthlyPage.Attributes["id"].Value); 
} 

注意,你可以只改變foreach申報monthlyPage作爲XmlNode代替object,它會做一個投給你。但是最好將它們放入通用集合中。

0

我將所有發生次數從List<object>更改爲List<XmlNode>。因此,代碼現在看起來像這樣: 主程序:

List<XmlNode> monthlyPages = new List<XmlNode>(); 
monthlyPages = StronyElementuStrukt(loginGuid, "8B35134E10A8432DB1A8C06A58427988"); 
foreach (XmlNodemonthlyPage in monthlyPages) 
{ 
    Console.WriteLine(monthlyPage.Attributes["id"].Value); 
} 

步驟:

public static List<XmlNode> StronyElementuStrukt(string LoginGUID, string LinkGUID) 
{ 
     List<XmlNode> listPages = new List<XmlNode>(); 

     XmlDocument document = new XmlDocument(); // tworzenie nowego obiektu - dokument xml z odpowiedzia serwera 
     document.LoadXml(response.Result); //wczytywanie xmla z odpowiedzia serwera do obiektu 
     XmlNode pageNode = document.SelectSingleNode("/IODATA/PAGES/PAGE"); //deklaracja noda xmlowego 

     if (pageNode != null) //jeżeli PAGE node istnieje 
     { 
      XmlNodeList nodeList = document.SelectNodes("//PAGE"); 

      foreach (XmlNode node in nodeList) 
      { 
       listPages.Add(node); 
      } 
      return listPages; 
     } 
    }