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

本文关键字:Java web 服务 响应 反序列化 Net | 更新日期: 2023-09-27 18:30:13

我正在使用.Net应用程序中基于java的web服务。问题出现了,当我从web服务获得反序列化响应时,响应中的一些字段带有null值,但如果我查看xml响应,则响应具有正确的值。所以我想主要的问题是.Net不能正确地反序列化响应。这是WSDL,我使用的方法是"GetForeignEnterpriseList"。

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>

这是.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无法反序列化"address"、"country"、"region"、"numberList"节点。

这是.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");
            }
        }
    }

有什么想法吗?

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

这是因为xml名称空间、所有ns2:、ns4:等

您可以从消息文本中删除这些内容,包括名称空间声明,这很简单,但很快。或者,你可以通过在你的对象中包括名称空间来正确地做到这一点,比如这个

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
{
 ....    
}

我还没试过,这只是猜测,祝你好运!

问题在于.Net对给定wsdl的解释。在WSDL中,Country实体的字段描述为

所以这里的主要问题是.Net.对minOccurs="0"maxOccurs="1"属性的解释

当soap响应为时

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

.Net无法正确反序列化它,并将country对象指定为null。我无法访问"country"对象的字段。我认为发生这种情况是因为.Net希望接收填充了所有字段的"country"对象,但只有两个字段。但当我使用NetBeans的web服务时,"country"对象包含所有字段。除了uuid和guid字段外,这些字段都为null。

目前,我复制了wsdl和所有xsd文件,并希望对它们进行修改,以便.Net进行适当的序列化。但下一个问题是如何正确地编辑它们?