There are a number of code issues in the question:
1. You are using XPath in GetElementsByTagName, just use the tag
2. You get only the first XmlNode in the XmlNodeCollection using [0]
3. Since you have only one XmlNode, you get only a string result to get the attribute, not a set of strings, which you then try to list through 4. Your foreach is broken, there is no type for the resulting object
Here is a snippet that will work:
var doc = new XmlDocument(); doc.Load("test.xml"); var items = doc.GetElementsByTagName("Item"); var xmlActions = new string[items.Count]; var xmlFileNames = new string[items.Count]; for (int i = 0; i < items.Count; i++) { var xmlAttributeCollection = items[i].Attributes; if (xmlAttributeCollection != null) { var action = xmlAttributeCollection["action"]; xmlActions[i] = action.Value; var fileName = xmlAttributeCollection["filename"]; xmlFileNames[i] = fileName.Value; } } foreach (var action in xmlActions) {
Or, if you donβt need all the actions and file names in the collection, before you act on them, you can simply act on each action / file name in a for loop.
source share