XML output in the wrong order

I wrote a simple WCF web service in C # that returns records from a database.

WCF uses the following method getQuestionnaireForID?id=(questionnaireID). Webservice returns all valid records from the database, however they look in the wrong order.

This is the order in which I would like the XML to be:

<QuestionnaireXML xmlns="http://schemas.datacontract.org/2004/07/QuestionnaireDescriptor" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <QuestionnaireName>Sample Questionnaire</QuestionnaireName>
        <Questions>
            <Question>
                <QuestionID>297</QuestionID>
                <QuestionName>What is your favorite type of tree?</QuestionName>
                <Answers>
                    <Answer>
                        <AnswerTitle>Beech</AnswerTitle>
                    </Answer>
                    <Answer>
                        <AnswerTitle>Oak</AnswerTitle>
                    </Answer>
                </Answers>
            </Question>
            <Question>
                <QuestionID>298</QuestionID>
                <QuestionName>Windows or Mac?</QuestionName>
                <Answers>
                    <Answer>
                        <AnswerTitle>Mac</AnswerTitle>
                    </Answer>
                    <Answer>
                        <AnswerTitle>Windows</AnswerTitle>
                    </Answer>
                </Answers>
            </Question>
        </Questions>
</QuestionnaireXML>

But instead, it is currently returning in the following order:

<QuestionnaireXML xmlns="http://schemas.datacontract.org/2004/07/QuestionnaireDescriptor" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <QuestionnaireName>Hello sir how do you do today?</QuestionnaireName>
    <Questions>
        <Question>
            <Answers>
                <Answer>
                    <AnswerTitle>Beech</AnswerTitle>
                </Answer>
                <Answer>
                    <AnswerTitle>Oak</AnswerTitle>
                </Answer>
            </Answers>
            <QuestionID>297</QuestionID>
            <QuestionName>What is your favorite type of tree?</QuestionName>
        </Question>
        <Question>
            <Answers>
                <Answer>
                    <AnswerTitle>Mac</AnswerTitle>
                </Answer>
                <Answer>
                    <AnswerTitle>Windows</AnswerTitle>
                </Answer>
            </Answers>
            <QuestionID>298</QuestionID>
            <QuestionName>Windows or Mac?</QuestionName>
        </Question>
    </Questions>
</QuestionnaireXML>

I am not sure if this is due to a non-fixed XML document or because I have my loops in the wrong order. Here is my WCF XML descriptor.

[DataContract]
    public class QuestionnaireXML
    {
        OsqarSQL getData;
        DataTable DT;
        private String _questionnaireName;
        private List<Question> _Question = new List<Question>();

        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 < 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<Question> 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<Answer> _Answer = new List<Answer>();

        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 < 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<Answer> 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; }
        }
    }

How can I solve this problem? Will this cause problems when parsing XML?

+5
source share
3 answers

This is the order in which I would like the XML to be:

? ? .

WCF DataContractSerializer - - . , XML, , , . , .

, Order (. MSDN), .

- XMLSerializer, , , DataContractSerializer.

, .

+5

, . XML, , , , , . , , , . XML .

0

:

. , , IList Generic IEnumerable, IList:

, , , .

See the “Extended Collection Rules” section “Collection Types in Data Documents” for all applicable rules and for possible explanations of why they exist.

-1
source

All Articles