XML& WCF POST请求:读取一些参数,其他参数不读取 [英] XML&WCF POST request: some parameters are read, other not

查看:120
本文介绍了XML& WCF POST请求:读取一些参数,其他参数不读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IIS服务器上有一个wcf restful服务。

I have a wcf restful service on a IIS server.

我已经制作了一些API,可以调用发送xml或json。

I have made some API, which can be called sending both xml or json.

我已经完成了我的C#课程,然后,我正在测试它。使用JSON是完美的,但我仍然存在XML请求的一些问题。

I've made my C# classes and then, I'm testing it. With JSON is perfect, but I have still some issues with XML request.

我想发送带有帖子的xml,这是我发送的xml:

I want to send the xml with a post and this is the xml I send:

<?xml version="1.0" encoding="utf-8" ?>
<SetClientiXML
  xmlns="http://tempuri.org/">
  <dati>
    <ArrayOfWrapClienti
      xmlns="http://schemas.datacontract.org/2004/07/MultipayOnline"
      xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <WrapClienti>
        <CODRETE>0018</CODRETE>
        <CODICE>20685</CODICE>
        <NOME>A.T.E.R. Azienda Territoriale</NOME>
        <INDIRIZZO>PIAZZA POZZA</INDIRIZZO>
        <CITTA>Verona</CITTA>
        <CAP>37123</CAP>
        <PROV>VR</PROV>
        <CODICEFISCALE>00223640236</CODICEFISCALE>
        <PIVA>223640236</PIVA>
        <EMAIL/>
        <ESPOSIZ_CONTABILE>937,02</ESPOSIZ_CONTABILE>
        <STATO>FALSE</STATO>
      </WrapClienti>
    </ArrayOfWrapClienti>
  </dati>
  <retista>3303903</retista>
  <hashedString>oklkokokokok</hashedString>
</SetClientiXML>

wcf读取retista和hashedString,但dati为空(0个元素) ),虽然我希望它有我发送的wrapClienti对象。

这是我的API的原型:

the wcf read well "retista" and "hashedString", but "dati" is empty (0 elements), while I expect it has got the "wrapClienti" object I sent. This is the prototype of my API:

    [OperationContract]
    [WebInvoke(UriTemplate = "SetClienti.xml", Method = "POST", BodyStyle=WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Xml)]
    GenericResponse SetClientiXML(List<WrapClienti> dati, string retista, string hashedString);


所以,问题是List是空的..为什么?如何编写xml以使列表可读?

问我是否可以给你更多细节。

So, the problem is that the List is empty.. why? How can I write the xml to make readable the list? Ask me if I can give to you more details.

更新:更奇怪!!

使用此xml:

<?xml version="1.0" encoding="utf-8" ?><SetClientiXML xmlns="http://tempuri.org/">
    <dati>
        <WrapClienti xmlns="http://schemas.datacontract.org/2004/07/MultipayOnline" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
            <CODRETE>0018</CODRETE> 
            <CODICE>20685</CODICE> 
            <NOME>A.T.E.R. Azienda Territoriale</NOME> 
            <INDIRIZZO>PIAZZA POZZA</INDIRIZZO> 
            <CITTA>Verona</CITTA> 
            <CAP>37123</CAP>
            <PROV>VR</PROV> 
            <CODICEFISCALE>00223640236</CODICEFISCALE> 
            <PIVA>223640236</PIVA> 
            <EMAIL/> 
            <ESPOSIZ_CONTABILE>937,02</ESPOSIZ_CONTABILE> 
            <STATO>FALSE</STATO> 
        </WrapClienti> 
    </dati>
    <retista>3303903</retista>
    <hashedString>oklkokokokok</hashedString>
</SetClientiXML>

wcf读取List的一些属性,其他..是nul !!!

the wcf read some attributes of the List, and other.. are nul!!!

我的WrapClienti我有一些属性。其中两个是:

I my WrapClienti I have a lof of attributes. Two of them are:

    private string p_CAP { get; set; }
    public string CAP
    {
        get
        {
            if (model == null)
                return p_CAP.ToSafeString();
            else
                return this.model.CAP.ToSafeString();
        }
        set { p_CAP = value; }
    }
    private string p_PROV { get; set; }
    public string PROV
    {
        get
        {
            if (model == null)
                return p_PROV.ToSafeString();
            else
                return this.model.PROV.ToSafeString();
        }
        set { p_PROV = value; }
    }

问题是,上面的xml和两个断点设置方法,只调用PROV的一套,而不是一个CAP!为什么?现在我真的变得疯狂......为什么会这样???

the problem is, with the xml above and with two breakpoint on the two set methods, only the set of PROV is called and, the one of CAP, not!!! Why? Now I'm really getting crazy... why this behavior??

此处的解决方案

推荐答案

这与XML中字段的排序有关。这听起来很奇怪,但是WCF DataContractSerializer对于在XML中遇到字段的顺序非常挑剔,但更糟糕的是,与它们在代码中定义的方式相比

This has to do with the ordering of the fields in your XML. It sounds very strange, but WCF DataContractSerializer is really fussy about the order in which the fields are encountered in the XML, but even worse, also in comparison to how they are defined in the code.

您可以看到,序列化程序希望按字母顺序定义字段,如果序列化类的实例,您会发现生成的XML字段位于按字母顺序。但是,在反序列化时,序列化程序会发现要反序列化的类型具有以错误顺序定义的字段。在这种情况下,行为似乎是随机的,但我认为它与CAP应该是遇到的第一个字段有关,而PROV应该是字母顺序的最后一个字段。

You see, the serializer wants the fields to be defined in alphabetical order, and if you serialize an instance of your class, you will find that the resulting XML fields are in alphabetical order. However, on deserialization, the serializer finds that the type you want to deserialize to has the fields defined in the "wrong" order. In this situation the behavior can seem random, but I think it has something to do with the fact that CAP should be the first field encountered, whereas PROV should be the last field, alphabetically.

所以你有两个选择:


  1. 重新排序你的XML和类中的字段按字母顺序排列,或者

  1. Reorder your XML and the fields in your class to be in alphabetical order, or

使用DataMemeber属性装饰类成员,并定义序列化的顺序。

Decorate your class members with the DataMemeber property, and define the order of serialization.

你可以这样做2:

[DataContract]
public class WrapClienti
{ 
    [DataMember(Order=1)]
    public string CAP { get; set; } 

    [DataMember(Order=2)]
    public string PROV { get; set; } 

    ...etc
}

这篇关于XML&amp; WCF POST请求:读取一些参数,其他参数不读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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