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.

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 );
This article has been dead for over six months. Start a new discussion instead.