0
Document miXML = new XDocument(

            new XElement("Alumnos",
                                new XElement("Alumno",
                                    new XAttribute("NoControl", "05231104"),
                                    new XElement("Nombre", "Edison García")),


                                 new XElement("Alumno",
                                    new XAttribute("NoControl", "05231106"),
                                    new XElement("Nombre", "Abraham Gomez García"),
                                    new XElement("Semestre", "9")),

                                new XElement("Alumno",
                                    new XAttribute("NoControl", "05231108"),
                                    new XElement("Nombre", "Alejandre Carvajal"),
                                    new XElement("Semestre", "7")),

                                new XElement("Alumno",
                                    new XAttribute("NoControl", "06231110"),
                                    new XElement("Nombre", "Luis Armando"),
                                    new XElement("Semestre", "10"))
                   )
            );


        StringReader sr = new StringReader(miXML.ToString());
        var testlinq = XElement.Load(sr).Elements();

        foreach(var abc in testlinq){

        var test2 = from p in abc.Descendants("Nombre") select p;
        var test3 = from p in abc.Descendants("Semestre") select p;
        }

This is the XML that is created. XML created

I want a list of dictionary List consists of {"Nombre","Edison Garcia"} , {{"Nombre", "Abraham Gomez García"} , {"semestre","9"}}

Any help would be appreciated.

Problems i am facing : 1)if there is no semsetre i am not able to skip it. It creates IEnumerable. 2)Getting the node and value dynamically instaed of hardcoding . Eg : var test2 = from p in abc.Descendants("Nombre") select p;

1 Answer 1

2

Try this:

var testlinq = XElement.Load(sr);   
var test = testlinq.Descendants("Alumno")
                   .Select(x => x.Descendants()
                                 .ToDictionary(y => y.Name, y => y.Value )
                   ).ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.