I have my main Program class that calls the StronyElementuStrukt
procedure
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:
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:
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:
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?)
Could you tell me how to reach to xml attributes in the foreach loop, please? Please ask if something is not clear enough.
Change the method to return a 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. But it is better to be specific with the types you are putting into a generic collection.