-2

I have the following non-alterable XML file :

<products>
    <product>
        <attributes>
            <att name="Name" value="TOTO" />
            <att name="Surname" value="Toto" />
            <att name="Age" value="10" />
        </attributes>
    </product>
    <product>
        <attributes>
            <att name="Name" value="TATA" />
            <att name="Surname" value="Tata" />
            <att name="Age" value="20" />
        </attributes>
    </product>
    <product>
        <attributes>
            <att name="Name" value="TITI" />
            <att name="Surname" value="Titi" />
            <att name="Age" value="30" />
        </attributes>
    </product>
</products>

Using C#, I need to extract the value of the value field for the nodes which value is equal to Name and Age, at a given index. Good example : inputing 2 would return a string containing TITI and another string containing 30.

For the moment, I'm using an XmlDocument to load the XML file, and a XmlNodeList with the GetElementsByTagName method to get all the attributes ; then, I can display those elements, but I don't manage to display only two of them, depending of attributes names and an index.

At best, I need a method like myXmlNodeList.Node["att"].Attributes["Name"].AtIndex(x).

Could anyone help me ?

Thesaurus Rex
  • 123
  • 1
  • 8
  • Surely you could have researched this before asking it here. – rory.ap Oct 22 '15 at 12:14
  • I have searched, but haven't find anything really close to my answer. I've found things about getting attributes thanks to nodes, nodes thanks to attributes, etc. Moreover, many methods use either XDocument or XmlDocument, and I don't think that mixing theses classes would be optimal. Also, I can't use an external library for this task. – Thesaurus Rex Oct 22 '15 at 12:16
  • Possible duplicate of [How do I read and parse an XML file in C#?](http://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c) – Nasreddine Oct 22 '15 at 12:18
  • @Nasreddine I've found this question and followed the proposed answer : reading XML inner text or attribute in a given XML file, ok. Beside this, how is it related to my question ? – Thesaurus Rex Oct 22 '15 at 12:23
  • Once you've parsed the XML you can do anything you want with it. for instance, make a function that take the `int` as a parameter and return the strings you want. – Nasreddine Oct 22 '15 at 12:24
  • I don't want to sound rude, but you are basically telling me to do what I said I haven't manage to do... :/ Starting from scratch and using the "duplicate" question's answer, I can only read the XML file and get the inner text of the first _att_, the problem coming from the fact that the only thing differentiating these _att_ being its _name_ value. – Thesaurus Rex Oct 22 '15 at 12:35

1 Answers1

0

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<products>" +
                    "<product>" +
                        "<attributes>" +
                            "<att name=\"Name\" value=\"TOTO\" />" +
                            "<att name=\"Surname\" value=\"Toto\" />" +
                            "<att name=\"Age\" value=\"10\" />" +
                        "</attributes>" +
                    "</product>" +
                    "<product>" +
                        "<attributes>" +
                            "<att name=\"Name\" value=\"TATA\" />" +
                            "<att name=\"Surname\" value=\"Tata\" />" +
                            "<att name=\"Age\" value=\"20\" />" +
                        "</attributes>" +
                    "</product>" +
                    "<product>" +
                        "<attributes>" +
                            "<att name=\"Name\" value=\"TITI\" />" +
                            "<att name=\"Surname\" value=\"Titi\" />" +
                            "<att name=\"Age\" value=\"30\" />" +
                        "</attributes>" +
                    "</product>" + 
                "</products>";

            XElement products = XElement.Parse(xml);

            var results = products.Elements("product").Select(x => new
            {
                name = x.Descendants("att").Where(y => y.Attribute("name").Value == "Name").Select(z => z.Attribute("value").Value).FirstOrDefault(),
                age = x.Descendants("att").Where(y => y.Attribute("name").Value == "Age").Select(z => (int)z.Attribute("value")).FirstOrDefault()
            }).ToList();

            string name = results[2].name;
            int age = results[2].age;

        }
    }


}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • This throws an error with the Select method : "'System.Collections.Generic.IEnumerable' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found". Thanks for your help though. – Thesaurus Rex Oct 22 '15 at 13:06
  • Code as posted works with Net 4.0. Make sure you have all the using statements at top of my code. I modified you original XML because it didn't have matching tags. – jdweng Oct 22 '15 at 15:36
  • It works, thank you. Also, I corrected the XML file in my question, thanks for noticing it ! By the way, I solved my problem using a heavily parametered XPath expression, but I assume it could have been done in an easier way. – Thesaurus Rex Oct 23 '15 at 07:25