.net - Trying to parse XML tree with Linq to XML (C#) -
I would like to reflect the XML tree in my object structure, but I'm making a lot of startup in XML using LINQ
I have the following structure XML:
& lt; Question & gt; & Lt; Question id = "q1" & gt; & Lt; Number & gt; 1 & lt; / Number & gt; & Lt; Text & gt; Some text 11 & lt; / Text & gt; & Lt; Answer & gt; & Lt; Answer & gt; & Lt; V & gt; Some text 11 & lt; / V & gt; & Lt; / Answer> & Lt; Answer & gt; & Lt; V & gt; Some text 11 & lt; / V & gt; & Lt; / Answer> & Lt; / Answer & gt; & Lt; / Question & gt; & Lt; Question id = "q2" & gt; & Lt; Number & gt; 2 & lt; / Number & gt; & Lt; Text & gt; Some text 2 & lt; / Text & gt; & Lt; Answer & gt; & Lt; Answer & gt; & Lt; V & gt; Some text is 22 & lt; / V & gt; & Lt; / Answer> & Lt; Answer & gt; & Lt; V & gt; Some text is 22 & lt; / V & gt; & Lt; / Answer> & Lt; / Answer & gt; & Lt; / Question & gt; & Lt; Question id = "Q3" & gt; & Lt; Number & gt; 3 & lt; / Number & gt; & Lt; Text & gt; Some text 3 & lt; / Text & gt; & Lt; Answer & gt; & Lt; Answer & gt; & Lt; V & gt; Some text 33 & lt; / V & gt; & Lt; / Answer> & Lt; Answer & gt; & Lt; V & gt; Some text 33 & lt; / V & gt; & Lt; / Answer> & Lt; Answer & gt; & Lt; V & gt; Some text 33 & lt; / V & gt; & Lt; AddDescription & gt; Some text 333 & lt; / AddDescription & gt; & Lt; Text box / & gt; & Lt; / Answer> & Lt; / Answer & gt; & Lt; / Question & gt; & Lt; / Question & gt; ... and I have the following classes:
public class questions {public string text {get; Set; } Public ILIST & Lt; Ender & G; Anwsers = new list & lt; Anwser & gt; (); } Public class Anwser {public string content {receives; Set; }} ... and I have created a (wrong) link query:
list & lt; Question & gt; Question = (xdoc.Element ("Survey") from XML, element ("question") element ("question") select new question () {text = xml.Element ("text"). Value, anwsers (Elements ("Answer"). Element ("Answer") Select the new Anwser () (content = anwsers.Element) Elements ("Question"). ("V"). Value}) .Oolist ()} ). ToList (); Of course all the anwsers of all questions added to each list in this way. How to solve it? I can imagine that this is simple but I have no idea :)
Thank you in advance!
Your code is not working because you are returning all the answer elements because you asked them You do not restrict them on the basis, you can add this restriction or instead of a subquery based on the document, you can create a subquery based on the question element.
list & lt; Question & gt; The question (xdoc.Element ("survey") with the question element ("question"). Element ("question") Select new question {text = question.Element ("text"). Value, anwsers = (Answer From the question in the Element ("Answer"), select the element ("Answer") New Anwser {content = answer.Element ("v") value}). ToList ()}). ToList ();
Comments
Post a Comment