使用 DataContractResolver 序列化接口时 XML 过于冗长

发布于 2024-11-23 19:58:08 字数 2357 浏览 6 评论 0原文

我已经大大缩短了问题,因为到目前为止我还没有得到答案。

基本上我正在尝试使用 DataContractSerializer 序列化接口。但是,当使用 DataContractResolver 执行此操作时,生成的 XML 确实非常冗长。我有一个接口 IAnimal 和一个具体实现 Cat。

如果我只是序列化 Cat,我会得到 XML:

<Cat xmlns="http://schemas.datacontract.org/2004/07/TestDataContractResolver.Concrete" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Species>feline</Species>
  <Weight>1</Weight>
</Cat>

但是,当在 DataContractResolver 的帮助下序列化 IAnimal 时,我会得到以下 XML:

<z:anyType i:type="a:TestDataContractResolver.Concrete.Cat" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="TestDataContractResolver.Concrete, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
  <Species xmlns="http://schemas.datacontract.org/2004/07/TestDataContractResolver.Concrete">feline</Species>
  <Weight xmlns="http://schemas.datacontract.org/2004/07/TestDataContractResolver.Concrete">1</Weight>
</z:anyType>

这是极其冗长的。为什么它将 xmlns 粘贴到每个属性元素中,而不仅仅是根元素中?我怎样才能让它不这样做呢?

顺便说一下,这是我的 DataContractSerializer 实现,基于 Youssef Moussaoui 的博客

public class ModelDataContractResolver : DataContractResolver
    {
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null) ?? Type.GetType(typeName + ", " + typeNamespace);
        }

        public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
        {
            if (knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace))
            {
                return true;
            }

            XmlDictionary dictionary = new XmlDictionary();
            typeName = dictionary.Add(type.FullName);
            typeNamespace = dictionary.Add(type.Assembly.FullName);

            return true;
        }
    }

I've shortened the question drastically as I've gotten no answers so far.

Basically I'm trying to serialize interfaces with the DataContractSerializer. But when using the DataContractResolver to do this, the generated XML is really verbose. I have an interface IAnimal and a concrete implementation Cat.

If I just serialize Cat I get the XML:

<Cat xmlns="http://schemas.datacontract.org/2004/07/TestDataContractResolver.Concrete" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Species>feline</Species>
  <Weight>1</Weight>
</Cat>

However, when serializing IAnimal with the help of a DataContractResolver, I get the following XML:

<z:anyType i:type="a:TestDataContractResolver.Concrete.Cat" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="TestDataContractResolver.Concrete, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
  <Species xmlns="http://schemas.datacontract.org/2004/07/TestDataContractResolver.Concrete">feline</Species>
  <Weight xmlns="http://schemas.datacontract.org/2004/07/TestDataContractResolver.Concrete">1</Weight>
</z:anyType>

Which is rediculously verbose. Why is it sticking xmlns's into every property element, not just the root element? And how do I make it not do that?

By the way, here is my implementation for the DataContractSerializer, based on Youssef Moussaoui's blog:

public class ModelDataContractResolver : DataContractResolver
    {
        public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
        {
            return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null) ?? Type.GetType(typeName + ", " + typeNamespace);
        }

        public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
        {
            if (knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace))
            {
                return true;
            }

            XmlDictionary dictionary = new XmlDictionary();
            typeName = dictionary.Add(type.FullName);
            typeNamespace = dictionary.Add(type.Assembly.FullName);

            return true;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文