REST-ful WCF 服务:反序列化 C# 字符串列表无法正常工作 [英] REST-ful WCF service: Deserializing C# List of Strings not working properly

查看:41
本文介绍了REST-ful WCF 服务:反序列化 C# 字符串列表无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 REST-ful WCF 服务,它返回一个 XML 响应.它由正确序列化和反序列化的对象组成,唯一的例外是节点上的 List 属性未正确反序列化.XML 看起来像:

I've got a REST-ful WCF service that returns back an XML response. It is comprised of objects that are serializing and de-serializing correctly, with the one exception that my List property on a node is not deserializing correctly. The XML looks like:

<ShippingGroups>
<ShippingGroup>
  <ShippingGroupId>
  b0b4d8a4-ff1f-4f02-a47c-263ef8ac861b</ShippingGroupId>
  <ShippingAddressId>
  63c0b52c-b784-4c27-a3e8-8adafba36add</ShippingAddressId>
  <LineItemIds xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <a:string>ccc0f986-52d5-453e-adca-8ff4513c1d84</a:string>
  </LineItemIds>
</ShippingGroup>

出现问题是因为我用于反序列化此 XML 的 C# 类需要 List LineItemIds.我可以通过手动删除该命名空间并删除 .

The problem arises because my C# class to deserialize this XML is expecting a List LineItemIds. I can get around this by manually removing that namespace and removing the .

是否有另一种方法可以解决这个问题,使其看起来像:

Is there another way around this, such that it would look like:

<ShippingGroups>
<ShippingGroup>
  <ShippingGroupId>
  b0b4d8a4-ff1f-4f02-a47c-263ef8ac861b</ShippingGroupId>
  <ShippingAddressId>
  63c0b52c-b784-4c27-a3e8-8adafba36add</ShippingAddressId>
  <LineItemIds>
    <string>ccc0f986-52d5-453e-adca-8ff4513c1d84</string>
  </LineItemIds>
</ShippingGroup>

推荐答案

我想我有一个答案.没有看到您的 DataContracts,我有点猜测您正在使用什么以及您的数据是如何构建的.但是这里...

I think I have an answer for you. Without seeing your DataContracts, I'm kind of guessing as to what you're using and how your data is structured. But here goes...

我为此使用了 VS2010、C#、WCF REST 和 .NET 4.

I am using VS2010, C#, WCF REST and .NET 4 for this.

默认情况下,您的集合或数组使用默认命名空间,以便在序列化时保持互操作性.因此,您的序列化按设计运行.

By default, your collection or array is using a default namespace in order to maintain interoperability when it gets serialized. So, your serialization is behaving as designed.

如果您创建自定义集合并在其上使用 CollectionDataContract 属性,则可以解决此问题.然后,您可以更好地控制它的序列化方式,包括其命名空间.这是来自 MSDN 的详细解释.

You can get around this if you create a custom collection and use the CollectionDataContract attribute on it. You then have more control over how it gets serialized, including its namespace. Here's a detailed explanation about this from MSDN.

所以我创建了一个自定义集合并使用了 CollectionDataContract 命名空间:

So I created a custom collection and used the CollectionDataContract namespace as such:

[CollectionDataContract(Namespace="")]
public class StringItem2 : Collection<string>
{
}

没有属性,因为这个集合只保存字符串.

There are no properties since this collection will just hold strings.

然后我的 DataContract 包含我的自定义字符串集合:

I then have my DataContract that contains my custom string collection:

[DataContract(Namespace="", IsReference=false)]
public class SampleItem
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string StringValue { get; set; }
    [DataMember]
    public StringItem2 StringItems2 { get; set; }
}

现在我已经完成了,我有了简单的 WCF RESTful 服务 (GET):

Now that I've done that, I have my simple WCF RESTful service (GET):

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1
{

    [WebGet(UriTemplate = "{id}")]
    public SampleItem Get(string id)
    {
        SampleItem si = new SampleItem()
        {
            Id = 10,
            StringValue = "foo",
            StringItems2 = new StringItem2() { "Hola", "como", "esta", "usted" }
        };
        return si;
    }

}

当我请求此服务时(http://localhost:xxxx/Service1/10),我得到以下 XML 作为响应:

When I request this service (http://localhost:xxxx/Service1/10), I get the following XML as a response:

<SampleItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
 <Id>10</Id> 
 <StringItems2>
  <string>Hola</string> 
  <string>como</string> 
  <string>esta</string> 
  <string>usted</string> 
 </StringItems2>
 <StringValue>foo</StringValue> 
</SampleItem>

希望这会有所帮助.请让我知道是否有我遗漏的其他详细信息,或者是否有更多问题.我会相应地更新我的答案.

Hopefully this helps. Please let me know if there are additional details that I missed or if there's more to the question. I'll update my answer accordingly.

这篇关于REST-ful WCF 服务:反序列化 C# 字符串列表无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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