WCF DataMember列表没有封闭元素 [英] WCF DataMember List<> without enclosing element

查看:72
本文介绍了WCF DataMember列表没有封闭元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下DataContract:

The following DataContract:

    [DataContract(Namespace = "http://namespace", Name = "Blarg")]
    public class Blarg
    {
        [XmlAttribute("Attribute")]
        public string Attribute{ get; set; }

        [DataMember(Name = "Record", IsRequired = false, Order = 4)]
        public List<Record> Record{ get; set; }
    }

序列化为此:

<Blarg Attribute="blah">
    <Record>
        <Record/>
        <Record/>
        <Record/>
    </Record>
</Blarg>

但是我想要这个:

<Blarg>
    <Record/>
    <Record/>
    <Record/>
<Blarg/>

DataContractSerializer似乎是在自动插入标头父级,我不希望这样做.

The DataContractSerializer seems to be inserting the header parent automagically and I don't want it.

我如何去掉包装纸<Record>?

推荐答案

我认为您无法做到这一点.

I don't think you can do that.

DataContractSerializer已针对速度进行了优化,并且在此过程中牺牲了一些灵活性和某些功能(例如XML属性).我认为您没有多少机会影响DCS-DCS会尽其所能并尽快完成它的工作.您可以非常整洁地定义进行序列化(使用[DataMember]属性,但是在如何序列化方面并没有真正的发言权.

The DataContractSerializer is optimized for speed, and in the process it sacrifices some flexibility and some features (like XML attributes). I don't think you have much chance to influence the DCS - it does its job as it sees fit, and as quickly as possible. You get to define quite neatly what to serialize (with the [DataMember] attribute, but you don't really have a say in how to serialize.

如果您需要更多控制权,则可以选择XmlSerializer-在这种情况下,序列化速度要慢10-15%,但是您可以控制数据形状等内容.但是即使在这种情况下,我不知道有什么方法可以告诉XML序列化器将一个集合序列化为一系列XML标记,而无需为该集合添加封闭标记.

If you need more control, you could pick the XmlSerializer instead - in that case, you have 10-15% slower serialization, but you can control things like the shape of the data etc. But even in this case - I am not aware of any way you can tell the XML serializer to serialize a collection into a series of XML tags without an enclosing tag for the collection.

这篇关于WCF DataMember列表没有封闭元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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