ich have a xml file with the following structure:
<layer1 name="this is layer1">
<messages>
<message name ="com_request">0</message>
<message name="send">1</message>
<message name="request">2</message>
</messages>
</layer1>
I try to collect all the message names in one indexer using the code:
SampleCollection<string> paramCollection = new SampleCollection<string>();
string pathxml = @"C:\myXML.xml";
int j=0;
XmlTextReader xmlin = new XmlTextReader(pathxml);
XmlDocument xmldoc = new XmlDocument();
XmlNode node = xmldoc.ReadNode(xmlin);
foreach (XmlNode item in node.ChildNodes)
{
paramCollection[j] = item.Attributes["message name"].Value;
Console.WriteLine(paramCollection[j]);
j++;
}
However it doesn’t work. Please help.
In addition to what others have mentioned (attribute name is wrong), also your node points to
layers1, who only has the direct childmessages, so there are nomessagenodes in your child collection. The same thing in LINQ is trivial – switch if you can: