-1

I am storing a questionnaire which is in xml format to a string type data field called Questionnaire. The database fields are contactID and Questionannire. I am doing this in an MVC application.Can someone tell me what should the ViewModel look like ?

The xml would like

<xml>
   <Question>What is your country of origin?/<Question>
   <Answer>United Kingdom </Answer> 
   <Question>What is your place of birth?</Question>
   <Answer>United States </Answer> 
</xml>
user3751248
  • 303
  • 4
  • 8
  • 18
  • A view model contains the properties you want to display/edit in the view, so whatever you want to display/edit, then create those properties. Its not clear why you have an xml file as opposed to saving the questions and user answers direct to the database so difficult to give you a answer. –  Oct 24 '14 at 06:33
  • Hi the reason why I am saving it as an example is to avoid using multiple tables to store that information. I would then have to create a Questions table , Answers table and then QuestionAnswer table. I just wanted to keep it simple as I wanted to store it as in a single column – user3751248 Oct 24 '14 at 09:14
  • Could you give me an example of how my view model should look like given the xml structure mentioned above as well my database table – user3751248 Oct 24 '14 at 09:15
  • I assure you it will be far simpler to create a table in your database than having to generate an xml file from your view model properties, then parse the xml file back to properties that you can use in you view model. –  Oct 24 '14 at 20:15
  • Thanks Stephen for your suggestion but my question of how the viewmodel should look like isn't answered yet. – user3751248 Oct 25 '14 at 15:49
  • You have 2 properties, `Question` and `Answer` so `public class MyViewModel { public string Question { get; set; } public string Answer { get; set; } }` and pass a list of `MyViewModel` to the view. –  Oct 25 '14 at 22:39
  • Your XML is going to cause a lot of problems.. for instance, how can you write a query that determines how many answers exist for question #2? There's no structure and thus, no organization... questions and answers can only be inferred by position in the xml, which is not particularly easy to deal with. You might be better off with xml like , etc... – Erik Funkenbusch Oct 28 '14 at 21:51

1 Answers1

0

Your viewmodel could look like this:

public class VM { public IEnumerable<QA> MyQuestionsAnswers {get;set; } }
//then you need a QA class
public class QA{
   public List<string> Questions {get;set;}
   public List<string> Answers {get;set;}
 }

Next you need to read the XML and put it into a List of QA...

XDocument document = XDocument.Load(@"XMLFile1.xml"); 

var TheQuestions = (from br in document.Descendants("Questions") 
                   select br).ToList();  
var TheAnswers = (from or in document.Descendants("Answers") 
                   select or).ToList(); 
var myQA = new QA{Questions=TheQuestions, Answers=TheAnswers}
JWP
  • 6,672
  • 3
  • 50
  • 74
  • That's a poor way to store questions and answers... by separating them into separate lists you lose the natural linkage of an object. Now you have to keep them in sync.. – Erik Funkenbusch Oct 28 '14 at 21:23
  • Agreed but I didn't feel like solving the entire problem for the op. I'm thinking by showing the Op how to decompose the problem he can finish it up. Maybe you can finish it up for him? – JWP Oct 28 '14 at 21:38
  • Besides the root of the issue is the layout of the XML. Both the question and answer should be contained in a field... – JWP Oct 28 '14 at 21:39