Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7419597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:05:57+00:00 2026-05-29T08:05:57+00:00

I have written a simple WCF web service in C# which returns records from

  • 0

I have written a simple WCF web service in C# which returns records from a database.

The WCF uses the following method: getQuestionnaireForID?id=(questionnaireID). The Webservice returns all the correct records from the database, however they appear to be in the wrong order.

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

<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 currently returns 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>

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.

[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 resolve this problem? Will it cause issues when trying to parse the XML?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:05:58+00:00Added an answer on May 29, 2026 at 8:05 am

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

    Why? Is this necessary? I’d wager it’s almost certainly not necessary.

    The WCF DataContractSerializer serializes – by default – in alphabetical order. You’ll note that the XML you are seeing has nodes ordered alphabetically. It does not matter which order you list your class properties.

    You can specify the order of elements using the Order attribute (see MSDN Data Member Order), if necessary.

    The other option is to use the XMLSerializer, which is more flexible but not as simple as the DataContractSerializer.

    Unless there is a genuine need to re-order these elements then I would not concern myself with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a simple WCF web service which is configured programmaticaly. It exposes
In my WCF web service I have been reading from the database with no
I have written a simple HelloWorld web service. Which takes an ArrayList as parameter.
I have written a simple WCF service that accepts and stores messages. It works
I have written a very simple WCF service, that worked fine (code below), then
I have written a simple jQuery.ajax function which loads a user control from the
I have a application written in C#, installed as a [WCF] windows service, which
I have written a simple REST API in WCF, and the authentication mechanism uses
I have a (very) simple WCF written in VB which I can build and
I've written a fairly simple little C# web service, hosted from a standalone EXE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.