C#如何在使用SOAP Web服务时阻止生成二维数组?

时间:2016-09-20 18:17:01

标签: c# web-services soap

我们正在开发SOAP Web Service,它必须返回以下示例XML结构:

<?xml version="1.0" encoding="UTF-8"?>
<response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <result code="2" service="2">
      <data>
         <input key="k2" keyTitle="kt2" value="v2" valueTitle="vt2" />
         <nested id="1">
            <data xmlns="InnerData">
               <input key="deb-num" keyTitle="nese" value="141" valueTitle="141" />
               <input key="deb-sum" keyTitle="" value="borcun meblegi" valueTitle="10.00" />
               <input key="deb-type" keyTitle="borcun neise" value="MA" valueTitle="MA" />
               <input key="title" keyTitle="titleee" value="10.00" valueTitle="10.00" />
            </data>
            <data xmlns="InnerData">
               <input key="deb-num" keyTitle="nese" value="141" valueTitle="141" />
               <input key="deb-sum" keyTitle="" value="borcun meblegi" valueTitle="10.00" />
               <input key="deb-type" keyTitle="borcun neise" value="MA" valueTitle="MA" />
               <input key="title" keyTitle="titleee" value="10.00" valueTitle="10.00" />
            </data>
         </nested>
      </data>
   </result>
</response>

我已经创建了没有问题的类结构。上面的示例结构是通过应用程序生成的。下面我列出了从Nested object

开始的部分类
public class Nested
{
    [System.Xml.Serialization.XmlAttribute(AttributeName = "id")]
    public string Id { get; set; }

    [System.Xml.Serialization.XmlElement(Namespace = "InnerData", ElementName = "data")]
    public Data[] Data { get; set; }

}

public class Data
{
    [System.Xml.Serialization.XmlElement(Namespace = "InnerData", ElementName = "input")]
    public Input[] Input { get; set; }
}

public class Input
{
    private string _key;

    private string _keyTitle;

    private string _value;

    private string _valueTitle;

    [System.Xml.Serialization.XmlAttribute(AttributeName = "key")]
    public string Key
    {
        get { return _key; }
        set { _key = value; }
    }

    [System.Xml.Serialization.XmlAttribute(AttributeName = "keyTitle")]
    public string KeyTitle
    {
        get { return _keyTitle; }
        set { _keyTitle = value; }
    }

    [System.Xml.Serialization.XmlAttribute(AttributeName = "value")]
    public string Value
    {
        get { return _value; }
        set { _value = value; }
    }

    [System.Xml.Serialization.XmlAttribute(AttributeName = "valueTitle")]
    public string ValueTitle
    {
        get { return _valueTitle; }
        set { _valueTitle = value; }
    }
}

问题与消费者有关。我为此测试目的创建了此Web服务的使用者。它会生成引用,将Data[] Data对象中的Nested转换为Input的二维数组。下面是生成的Reference.cs中的Nested对象:

    /// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1064.2")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public partial class Nested : object, System.ComponentModel.INotifyPropertyChanged {

    private Input1[][] dataField;

    private string idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayAttribute(Namespace="InnerData", Order=0)]
    [System.Xml.Serialization.XmlArrayItemAttribute("input", typeof(Input1), IsNullable=false)]
    public Input1[][] data {
        get {
            return this.dataField;
        }
        set {
            this.dataField = value;
            this.RaisePropertyChanged("data");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
            this.RaisePropertyChanged("id");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

如何禁用将Data个对象Data[] Data(包含Input个对象的数组)的数组转换为Input个对象Input1[][]的二维数组在产生消费者的同时?

1 个答案:

答案 0 :(得分:0)

我可以通过向Data类添加假字段来找到解决方法。

public class Data
{
    // It is a workaround to prevent consumer generate two dimensional array of Input (Input[][])
    // instead of creating the array of Input inside the Data.
    // This is a fake field and is not used anywhere
    [System.Xml.Serialization.XmlElement(ElementName = "reserve")]
    public string Reserve { get; set; }

    [System.Xml.Serialization.XmlElement(ElementName = "input")]
    public Input[] Input { get; set; }
}