Basically, I am trying to grab data from an XML file, and set it as a var or int. I cannot for the life of me get it to work. It seems to be copying a string of "System.IEnumerated..." or something like that instead of what the actual value of the attribute is.

My XML file is structured as such:

<Army>
<Unit childIndex="0">
<other stuff within unit>
</other stuff within unit>
</Unit>
</Army>

And there are multiple Unit tags within Army that have unique attributes and elements within them.

The program is taking say, the first Unit in Army and selecting it. Then I need to take the childIndex and grab its value to set as an int or var to use within my program. Any ideas?


Here is most of that section of code:

// finding the row index of the button you clicked         
                  var vCurrentlySelectedRow = dataGridView1.CurrentRow.Index;

                     XDocument vdocument = XDocument.Load(textBox1.Text);

                    
                     var vUnits = from Unit in vdocument.Descendants("Unit")
                                 
                               select new
                               {
                                   vName = Unit.Element("Name").Value,  
                                   vType = Unit.Attribute("type").Value,                                                                 
                                   vTypeIndex = Unit.Attribute("typeindex").Value,
                                   vChildIndex = Unit.Attribute("childID").Value,
                               };
                  
                    
                  string  vIndexString = vCurrentlySelectedRow.ToString();      
    
               
                     // Comparing the row index and type of the units in the XML file and tieing it to a var           
                     var vClickedUnit = from Unit in vUnits
                                           where Unit.vTypeIndex == vIndexString
                                           where Unit.vType == textBox2.Text
                                              select Unit;

This just basically finds out which units button was clicked in my program, so it knows what Unit to display from the XML file.

Thanks in advance for any help.

Dani AI

Generated

Quick diagnosis and the two most common causes of the "System.Linq.Enumerable..." / "System.IEnumerable..." text showing up instead of the attribute value:

  • The code is ending up with an IEnumerable (a LINQ query) and then calling ToString() on that sequence or otherwise inspecting the sequence object instead of a single attribute value. Materialize a single element (First/Single) or enumerate it before reading the attribute.
  • A typo/mismatch in attribute names. In ' XML the attribute is spelled childIndex but the posted selector uses a different name. Attribute names are exact (including case) when using LINQ to XML, so a misspelled name yields null or an empty sequence.

Safe pattern to pick a single Unit and get an int attribute value (illustrates casting and parsing without calling ToString on a sequence):

var selected = doc.Descendants("Unit")
    .FirstOrDefault(u => (string)u.Attribute("type") == desiredType
                       && (string)u.Attribute("typeIndex") == rowIndexString);

if (selected != null)
{
    string raw = (string)selected.Attribute("childIndex");   // safe cast returns null if missing
    if (int.TryParse(raw, out int childIndex))
    {
        // childIndex is a usable int here
    }
}

Practical troubleshooting tips:

  • Inspect the LINQ query result in the debugger: if it shows an IEnumerable, call FirstOrDefault/SingleOrDefault or iterate it; do not rely on ToString().
  • Verify attribute names exactly (spelling and case). Confirm the XML actually contains the attribute for the selected element before calling .Value.
  • Use (string)element.Attribute("name") or the null-conditional patterns and int.TryParse to avoid null refs and to safely convert to numbers.
  • As noted, combine conditions into one predicate (or use multiple where clauses) — either approach is fine so long as the result is materialized before reading a value.

Reference: XElement.Attribute, XAttribute.Value, int.TryParse.

Recommended Answers

All 3 Replies

Sorry! Do you have a problem to compare attribute's value?

var vClickedUnit = from Unit in vUnits
   where Unit.vTypeIndex == vIndexString && Unit.vType == textBox2.Text
   select Unit;

Sorry! Do you have a problem to compare attribute's value?

var vClickedUnit = from Unit in vUnits
   where Unit.vTypeIndex == vIndexString && Unit.vType == textBox2.Text
   select Unit;

Thanks for the reply. But no, I'm not having problems comparing the attributes. I just need to take the value of an attribute and set it as a variable.

Take a look:

string path = @"csample.xml";


            XDocument doc = XDocument.Load(path);

            var result = from ele in doc.Descendants("Data")
                         select new
                         {
                             ID = ele.Attribute("ID").Value,
                             Date=ele.Attribute("Date").Value,
                             TopicStarted= ele.Element("Topic").Attribute("Started").Value,
                             Title= ele.Element("Topic").Element("Title").Value,
                             Description= ele.Element("Topic").Element("Description").Value 

                         };

            foreach (var t in result)
                Console.WriteLine(t.ID + " " + t.Date + " " + t.TopicStarted + " " + t.Title + " " + t.Description );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.