简体   繁体   English

.Net(C#)无法反序列化Java Web服务响应

[英].Net (C#) cannot deserialize Java web service response

I am consuming java based web service from .Net application. 我正在从.Net应用程序使用基于Java的Web服务。 The problem arise, when I got deserialized response from the web service, some of the fields in response comes with null values, but if I look at xml response, response has its proper values. 问题出现了,当我从Web服务获得反序列化响应时,响应中的某些字段带有空值,但是如果我查看xml响应,则响应具有其适当的值。 So I guess main problem is that .Net cannot deserialize response properly. 因此,我想主要的问题是.Net无法正确反序列化响应。 Here is WSDL , the method I am using is "GetForeignEnterpriseList". 这是WSDL ,我使用的方法是“ GetForeignEnterpriseList”。

XML Response is XML响应为

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" />
  <S:Body>
    <ns6:getForeignEnterpriseListResponse xmlns="http://api.vetrf.ru/schema/base" xmlns:ns2="http://api.vetrf.ru/schema/argus" xmlns:ns3="http://api.vetrf.ru/schema/institutions" xmlns:ns4="http://api.vetrf.ru/schema/ikar" xmlns:ns5="http://api.vetrf.ru/schema/base/register" xmlns:ns6="http://api.vetrf.ru/schema/argus/enterprise/ws-definitions" xmlns:ns7="http://api.vetrf.ru/schema/argus/ws-definitions">
      <ns2:enterpriseList offset="0" total="14058" count="1">
        <ns2:enterprise>
          <uuid>06e4a78e-053d-11e1-99b4-d8d385fbc9e8</uuid>
          <guid>cb1483ab-2c7d-db46-445c-09849ae9b761</guid>
          <active>true</active>
          <last>true</last>
          <status>100</status>
          <createDate>2009-10-08T12:50:47+04:00</createDate>
          <updateDate>2009-10-08T12:50:47+04:00</updateDate>
          <ns2:name>Cargill Meat Solutions Corporation</ns2:name>
          <ns2:englishName>
          </ns2:englishName>
          <ns2:activity>Убой КРС, разделка, хранение говядины</ns2:activity>
          <ns2:englishActivity>Cattle slaughter, Beef cutting, Beef storage</ns2:englishActivity>
          <ns4:address>
            <ns4:addressView>Friona</ns4:addressView>
            <ns4:enAddressView>
            </ns4:enAddressView>
          </ns4:address>
          <ns4:country>
            <uuid>91134526-4373-ec59-4a1e-e0e99bd50b7b</uuid>
            <guid>cac8a802-3c65-397d-895d-c0495bf6ea61</guid>
          </ns4:country>
          <ns4:region>
            <uuid>c7bad316-6564-787c-c502-6a9e8afdc093</uuid>
            <guid>bbee471c-0548-7190-cca9-5897eedbeac3</guid>
            <ns4:hasStreets>false</ns4:hasStreets>
          </ns4:region>
          <ns2:numberList>
            <ns2:enterpriseNumber>86 Е</ns2:enterpriseNumber>
          </ns2:numberList>
        </ns2:enterprise>
      </ns2:enterpriseList>
    </ns6:getForeignEnterpriseListResponse>
  </S:Body>
</S:Envelope>

This is .Net generated class for enterprise node I need to get. 这是我需要获取的.Net生成的企业节点类。

 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18408")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://api.vetrf.ru/schema/argus")] public partial class Enterprise : GenericVersioningEntity { private string nameField; private string englishNameField; private string activityField; private string englishActivityField; private Address addressField; private Country countryField; private Region regionField; private District districtField; private string[] numberListField; private string typeField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=0)] public string name { get { return this.nameField; } set { this.nameField = value; this.RaisePropertyChanged("name"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=1)] public string englishName { get { return this.englishNameField; } set { this.englishNameField = value; this.RaisePropertyChanged("englishName"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=2)] public string activity { get { return this.activityField; } set { this.activityField = value; this.RaisePropertyChanged("activity"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=3)] public string englishActivity { get { return this.englishActivityField; } set { this.englishActivityField = value; this.RaisePropertyChanged("englishActivity"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=4)] public Address address { get { return this.addressField; } set { this.addressField = value; this.RaisePropertyChanged("address"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=5)] public Country country { get { return this.countryField; } set { this.countryField = value; this.RaisePropertyChanged("country"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=6)] public Region region { get { return this.regionField; } set { this.regionField = value; this.RaisePropertyChanged("region"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=7)] public District district { get { return this.districtField; } set { this.districtField = value; this.RaisePropertyChanged("district"); } } /// <remarks/> [System.Xml.Serialization.XmlArrayAttribute(Order=8)] [System.Xml.Serialization.XmlArrayItemAttribute("enterpriseNumber", IsNullable=false)] public string[] numberList { get { return this.numberListField; } set { this.numberListField = value; this.RaisePropertyChanged("numberList"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(DataType="integer", Order=9)] public string type { get { return this.typeField; } set { this.typeField = value; this.RaisePropertyChanged("type"); } } } 

.Net cannot deserialize "address", "country", "region", "numberList" nodes. .Net不能反序列化“地址”,“国家”,“区域”,“ numberList”节点。

And this is example of how .Net generated class for country node which comes as null. 这是.Net如何为国家节点生成类(为null)的示例。

 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18408")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://api.vetrf.ru/schema/ikar")] public partial class Country : GenericVersioningEntity { private string nameField; private string fullNameField; private string englishNameField; private string codeField; private string code3Field; private bool isRussiaField; private bool isRussiaFieldSpecified; private bool isCustomsUnionField; private bool isCustomsUnionFieldSpecified; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=0)] public string name { get { return this.nameField; } set { this.nameField = value; this.RaisePropertyChanged("name"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=1)] public string fullName { get { return this.fullNameField; } set { this.fullNameField = value; this.RaisePropertyChanged("fullName"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=2)] public string englishName { get { return this.englishNameField; } set { this.englishNameField = value; this.RaisePropertyChanged("englishName"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=3)] public string code { get { return this.codeField; } set { this.codeField = value; this.RaisePropertyChanged("code"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=4)] public string code3 { get { return this.code3Field; } set { this.code3Field = value; this.RaisePropertyChanged("code3"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=5)] public bool isRussia { get { return this.isRussiaField; } set { this.isRussiaField = value; this.RaisePropertyChanged("isRussia"); } } /// <remarks/> [System.Xml.Serialization.XmlIgnoreAttribute()] public bool isRussiaSpecified { get { return this.isRussiaFieldSpecified; } set { this.isRussiaFieldSpecified = value; this.RaisePropertyChanged("isRussiaSpecified"); } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Order=6)] public bool isCustomsUnion { get { return this.isCustomsUnionField; } set { this.isCustomsUnionField = value; this.RaisePropertyChanged("isCustomsUnion"); } } /// <remarks/> [System.Xml.Serialization.XmlIgnoreAttribute()] public bool isCustomsUnionSpecified { get { return this.isCustomsUnionFieldSpecified; } set { this.isCustomsUnionFieldSpecified = value; this.RaisePropertyChanged("isCustomsUnionSpecified"); } } } 

Any ideas? 有任何想法吗?

Its because of the xml namespaces, all of the ns2: , ns4: etc. 由于xml名称空间,所有ns2:,ns4:等。

You could strip these out of the message text including the namespace declarations, hacky but quick. 您可以将这些内容从消息文本中删除,包括名称空间声明,虽然有些笨拙但又很快。 Or you could do it properly by including the names spaces in your object something like this 或者您可以通过在对象中包含名称空间来正确地做到这一点,例如

XmlRoot(Namespace = "http://api.vetrf.ru/schema/")]
public class Enterprise
{
    [XmlElement(Namespace = "http://api.vetrf.ru/schema/ikar")]
    public Address Address{ get; set; }
    [XmlElement(Namespace = "http://api.vetrf.ru/schema/ikar")]
    public Country Country { get; set; }

   ...
}

XmlRoot(Namespace = "http://api.vetrf.ru/schema/ikar")]
public class Address
{
 ....    
}

havent tried this so is a guess, good luck! 还没有尝试过,这是一个猜测,祝您好运!

Problem is in interpretation of given wsdl by .Net. 问题在于.Net对给定wsdl的解释。 In WSDL fields of Country entity are described as 在国家实体的WSDL中,字段描述为

So main problem here is in interpretation of minOccurs="0" maxOccurs="1" attributes by .Net. 因此,这里的主要问题是.Net对minOccurs =“ 0” maxOccurs =“ 1”属性的解释。

When soap response comes as 当肥皂反应来

 <ns4:country> <uuid>91134526-4373-ec59-4a1e-e0e99bd50b7b</uuid> <guid>cac8a802-3c65-397d-895d-c0495bf6ea61</guid> </ns4:country> 

.Net can't deserialize it properly and gives country object as null. .Net无法对其进行反序列化,因此将country对象设置为null。 And i can't access fields of 'country' object. 而且我无法访问“国家”对象的字段。 I think it happens because .Net expects to receive "country" object with all fields populated, but there are only two fields. 我认为发生这种情况是因为.Net希望接收填充了所有字段的“国家”对象,但是只有两个字段。 But when I consume web service by NetBeans, 'country' object comes with all fields. 但是,当我使用NetBeans使用Web服务时,“ country”对象随所有字段一起提供。 Those fields are null except uuid and guid fields. 这些字段为空,除了uuid和guid字段。

Currently I copied wsdl and all xsd files, and want to modify them for .Net proper serialization. 目前,我复制了wsdl和所有xsd文件,并希望对其进行修改以实现.Net适当的序列化。 But next problem is how to edit them correctly? 但是下一个问题是如何正确编辑它们?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM