title: XML Output In the Wrong Order
body: <p>I have written a simple WCF web service in C# which returns records from a database.</p>
  
  <p>The WCF uses the following method: <code>getQuestionnaireForID?id=(questionnaireID)</code>. The Webservice returns all the correct records from the database, however they appear to be in the wrong order.</p>
  
  <p>This is the order I would like the XML to be in:</p>
  
  <pre><code>&lt;QuestionnaireXML xmlns="http://schemas.datacontract.org/2004/07/QuestionnaireDescriptor" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;QuestionnaireName&gt;Sample Questionnaire&lt;/QuestionnaireName&gt;
          &lt;Questions&gt;
              &lt;Question&gt;
                  &lt;QuestionID&gt;297&lt;/QuestionID&gt;
                  &lt;QuestionName&gt;What is your favorite type of tree?&lt;/QuestionName&gt;
                  &lt;Answers&gt;
                      &lt;Answer&gt;
                          &lt;AnswerTitle&gt;Beech&lt;/AnswerTitle&gt;
                      &lt;/Answer&gt;
                      &lt;Answer&gt;
                          &lt;AnswerTitle&gt;Oak&lt;/AnswerTitle&gt;
                      &lt;/Answer&gt;
                  &lt;/Answers&gt;
              &lt;/Question&gt;
              &lt;Question&gt;
                  &lt;QuestionID&gt;298&lt;/QuestionID&gt;
                  &lt;QuestionName&gt;Windows or Mac?&lt;/QuestionName&gt;
                  &lt;Answers&gt;
                      &lt;Answer&gt;
                          &lt;AnswerTitle&gt;Mac&lt;/AnswerTitle&gt;
                      &lt;/Answer&gt;
                      &lt;Answer&gt;
                          &lt;AnswerTitle&gt;Windows&lt;/AnswerTitle&gt;
                      &lt;/Answer&gt;
                  &lt;/Answers&gt;
              &lt;/Question&gt;
          &lt;/Questions&gt;
  &lt;/QuestionnaireXML&gt;
  </code></pre>
  
  <p>But instead it <strong>currently</strong> returns in the following order:</p>
  
  <pre><code>&lt;QuestionnaireXML xmlns="http://schemas.datacontract.org/2004/07/QuestionnaireDescriptor" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;QuestionnaireName&gt;Hello sir how do you do today?&lt;/QuestionnaireName&gt;
      &lt;Questions&gt;
          &lt;Question&gt;
              &lt;Answers&gt;
                  &lt;Answer&gt;
                      &lt;AnswerTitle&gt;Beech&lt;/AnswerTitle&gt;
                  &lt;/Answer&gt;
                  &lt;Answer&gt;
                      &lt;AnswerTitle&gt;Oak&lt;/AnswerTitle&gt;
                  &lt;/Answer&gt;
              &lt;/Answers&gt;
              &lt;QuestionID&gt;297&lt;/QuestionID&gt;
              &lt;QuestionName&gt;What is your favorite type of tree?&lt;/QuestionName&gt;
          &lt;/Question&gt;
          &lt;Question&gt;
              &lt;Answers&gt;
                  &lt;Answer&gt;
                      &lt;AnswerTitle&gt;Mac&lt;/AnswerTitle&gt;
                  &lt;/Answer&gt;
                  &lt;Answer&gt;
                      &lt;AnswerTitle&gt;Windows&lt;/AnswerTitle&gt;
                  &lt;/Answer&gt;
              &lt;/Answers&gt;
              &lt;QuestionID&gt;298&lt;/QuestionID&gt;
              &lt;QuestionName&gt;Windows or Mac?&lt;/QuestionName&gt;
          &lt;/Question&gt;
      &lt;/Questions&gt;
  &lt;/QuestionnaireXML&gt;
  </code></pre>
  
  <p>Im not sure if this is due to an unstyled XML document or because I have my loops in the wrong order. 
  Here is my WCF xml descriptor.</p>
  
  <pre><code>[DataContract]
      public class QuestionnaireXML
      {
          OsqarSQL getData;
          DataTable DT;
          private String _questionnaireName;
          private List&lt;Question&gt; _Question = new List&lt;Question&gt;();
  
          public QuestionnaireXML(int QuestionnaireID)
          {
              // Loop through datatable for questionIDs and load the questions
              getData = new OsqarSQL();
              _questionnaireName = getData.GetQuestionnaireName(QuestionnaireID);
              DT = getData.GetQuestionIDWCF(QuestionnaireID);
              for (int i = 0; i &lt; DT.Rows.Count; i++)
              {
                  _Question.Add(new Question(Int32.Parse(DT.Rows[i][0].ToString())));
              }
          }
  
          // Define DataMembers for XML output
          [DataMember]
          public String QuestionnaireName
          {
              get { return _questionnaireName; }
              set { _questionnaireName = value; }
          }
  
          [DataMember]
          public List&lt;Question&gt; Questions
          {
              get { return _Question; }
              set { _Question = value; }
          }
      }
  
      [DataContract]
      public class Question
      {
          private Int32 _questionId;
          private String _questionName;
          OsqarSQL getData;
          DataTable DT;
          DataTable AT;
          private List&lt;Answer&gt; _Answer = new List&lt;Answer&gt;();
  
          public Question(int QuestionID)
          {
              getData = new OsqarSQL();
              DT = getData.GetQuestionNameWCF(QuestionID);
              _questionId = (int)DT.Rows[0][0];
              _questionName = DT.Rows[0][1].ToString();
              AT = getData.GetAnswerTitle(QuestionID);
              for (int i = 0; i &lt; AT.Rows.Count; i++)
              {
                  _Answer.Add(new Answer(Int32.Parse(AT.Rows[i][0].ToString())));
              }
          }
  
          // Define DataMembers for XML output
          [DataMember]
          public Int32 QuestionID
          {
              get { return _questionId; }
              set { _questionId = value; }
          }
  
          [DataMember]
          public String QuestionName
          {
              get { return _questionName; }
              set { _questionName = value; }
          }
  
          [DataMember]
          public List&lt;Answer&gt; Answers
          {
              get { return _Answer; }
              set { _Answer = value; }
          }
      }
  
      [DataContract]
      public class Answer
      {
          private Int32 _answerId;
          private String _answerTitle;
          OsqarSQL getData;
          DataTable DT;
  
          // Constructor Logic
          public Answer(int AnswerID)
          {
              getData = new OsqarSQL();
              DT = getData.GetAnswerTitleWcf(AnswerID);
              _answerTitle = DT.Rows[0][1].ToString();   
          }
  
          // Define DataMembers for XML output
          [DataMember]
          public String AnswerTitle
          {
              get { return _answerTitle; }
              set { _answerTitle = value; }
          }
      }
  </code></pre>
  
  <p>How can I resolve this problem? Will it cause issues when trying to parse the XML?</p>