如何序列化XML到asp.net网页API所需的格式 [英] How to serialize xml into desired format in asp.net web api

查看:91
本文介绍了如何序列化XML到asp.net网页API所需的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在asp.net的MVC 4个Web API。我有一个类一样,

I am working on asp.net mvc 4 web api. I have a class like,

public class Quiz
{
public int QuizId{get; set;}
public string title{get; set;}
...
...
}

和我现在想找回quizs的列表中,这样写道:我喜欢,

and now i am trying to retrieve list of quizs so i wrote like,

public List<Quiz> GetQuizs()
{
return repository.ListQuizs();
}

我需要XML响应,所以我必须在webapi.config文件进行配置一样,

i need xml response so i have made configuration in webapi.config file like,

config.Formatters.XmlFormatter.UseXmlSerializer = true;

和我得到的回应一样,

<ArrayOfQuiz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Quiz>
<QuizId>4</QuizId>
<title>Master Minds</title>
</Quiz>
<Quiz>
<QuizId>5</QuizId>
<title>Master Minds</title>
</Quiz>
</ArrayOfQuiz>

但我希望像

<Quizs>
<Quiz>
<QuizId>4</QuizId>
<title>Master Minds</title>
</Quiz>
<Quiz>
<QuizId>5</QuizId>
<title>Master Minds</title>
</Quiz>
</Quiz>

我试图像,

public class quizs:List<Quiz>{}
public class Quiz
{
//properties here
}

但我无法quizs列表加载到quizs类。请指导我。

but i am unable to load list of quizs into quizs class. please guide me.

推荐答案

是否有任何理由为什么你在DataContract serialiszer使用XmlSerializer?

Is there any reason why you are using the XmlSerializer over the DataContract serialiszer?

如果没有你可以达到你想要的东西,像这样:

If not you can achieve what you want like so:


  1. 删除config.Formatters.XmlFormatter.UseXmlSerializer = TRUE;从你的配置

  2. 添加一个引用到System.Runtime.Serialisation

  3. 声明你的类和控制器

code

[DataContract(Namespace = "")]
public class Quiz
{
    [DataMember]
    public int QuizId { get; set; }
    [DataMember(Name = "title")]
    public string Title { get; set; }
}

[CollectionDataContract(Name = "Quizs", Namespace = "")]
public class QuizCollection : List<Quiz>
{
}

public class QuizsController : ApiController
{
    public QuizCollection Get()
    {
        return new QuizCollection
                       {
                           new Quiz {QuizId = 4, Title = "Master Minds"},
                           new Quiz {QuizId = 5, Title = "Another Title"}
                       };
    }
}

最后使用HTML头接受:应用程序/ XML致电您的服务

Finally call your service using the html header "accept : application/xml"

和你的结果应该是这样的:

And your result should be like:

<Quizs xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Quiz>
        <QuizId>4</QuizId>
        <title>Master Minds</title>
        </Quiz>
    <Quiz>
        <QuizId>5</QuizId>
        <title>Another Title</title>
    </Quiz>
</Quizs>

关于你的命名空间NB。他们是如何设置到命名空间=属性中删除它们。你要蒋张关系删除的xmlns:I =htt​​p://www.w3.org/2001/XMLSchema-instance它需要有让XML处理空值。

Regarding your namespaces NB. how they are set to namespace = "" in attributes to remove them. You are going to strugle to remove xmlns:i="http://www.w3.org/2001/XMLSchema-instance" it needs to be there to allow the XML to handle null values.

有关的datacontract串行更多信息集合,都支持这里看到

For more information on the datacontract serializers support for collections see here

这篇关于如何序列化XML到asp.net网页API所需的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆