草庐IT

c# - XML 输出顺序错误

coder 2024-06-26 原文

我用 C# 编写了一个简单的 WCF Web 服务,它从数据库返回记录。

WCF 使用以下方法:getQuestionnaireForID?id=(questionnaireID)。 Web 服务从数据库返回所有正确的记录,但它们的顺序似乎有误。

这是我希望 XML 的顺序:

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

但它当前按以下顺序返回:

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

我不确定这是由于未设置样式的 XML 文档还是因为我的循环顺序错误。 这是我的 WCF xml 描述符。

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

我该如何解决这个问题?尝试解析 XML 时会导致问题吗?

最佳答案

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

为什么?这是必要的吗?我敢打赌这几乎肯定没有必要。

WCF DataContractSerializer 默认按字母顺序序列化。您会注意到您看到的 XML 中的节点是按字母顺序排列的。列出类属性的顺序并不重要。

如有必要,您可以使用 Order 属性指定元素的顺序(参见 MSDN Data Member Order )。

另一种选择是使用 XMLSerializer,它更灵活但不如 DataContractSerializer 简单。

除非真的需要重新排列这些元素,否则我不会关心它。

关于c# - XML 输出顺序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9185565/

有关c# - XML 输出顺序错误的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  3. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  4. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  5. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  6. ruby - 如何进行排列以有效地定制输出 - 2

    这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][

  7. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  8. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  9. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  10. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

随机推荐