[英]C# cannot get values from list of objects
I have my main Program class that calls the StronyElementuStrukt
procedure 我有主程序类,该类调用
StronyElementuStrukt
过程
List<object> monthlyPages = new List<object>();
monthlyPages = StronyElementuStrukt(loginGuid, "8B35134E10A8432DB1A8C06A58427988");
Here is the procedure - a method that builds a list of xml nodes and returns it to the main Program class: 这是过程-一种构建xml节点列表并将其返回到主Program类的方法:
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;
}
}
In the main Program Class I need to pick up value of xml id
attribute, I'm trying to do it like this: 在主程序类中,我需要选择xml
id
属性的值,我正在尝试这样做:
foreach (object monthlyPage in monthlyPages)
{
Console.WriteLine(monthlyPage.Attributes["id"].Value);
}
The problem is that when I try to get the id
I get the following error: 问题是,当我尝试获取
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?)
错误6'object'不包含'Attributes'的定义,找不到扩展方法'Attributes'接受类型为'object'的第一个参数(您是否缺少using指令或程序集引用?)
Could you tell me how to reach to xml attributes in the foreach loop, please? 您能否告诉我如何在foreach循环中访问xml属性? Please ask if something is not clear enough.
请询问是否不清楚。
Change the method to return a List<XmlNode>
. 更改方法以返回
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;
}
Then this will work. 然后这将起作用。
List<XmlNode> monthlyPages = StronyElementuStrukt(
loginGuid,
"8B35134E10A8432DB1A8C06A58427988");
foreach (XmlNode monthlyPage in monthlyPages)
{
Console.WriteLine(monthlyPage.Attributes["id"].Value);
}
Note that you could just change the foreach
to declare monthlyPage
as XmlNode
instead of object
and it will do a cast for you. 注意,您可以更改
foreach
以将monthlyPage
声明为XmlNode
而不是object
,它将为您进行monthlyPage
。 But it is better to be specific with the types you are putting into a generic collection. 但是最好是将您要放入通用集合中的类型具体化。
I changed all occurences from List<object>
to List<XmlNode>
. 我将所有事件从
List<object>
更改为List<XmlNode>
。 So the code now looks like this: Main Program: 现在代码如下所示:Main Program:
List<XmlNode> monthlyPages = new List<XmlNode>();
monthlyPages = StronyElementuStrukt(loginGuid, "8B35134E10A8432DB1A8C06A58427988");
foreach (XmlNodemonthlyPage in monthlyPages)
{
Console.WriteLine(monthlyPage.Attributes["id"].Value);
}
Procedure: 程序:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.