2016-09-20 66 views
0

我們正在開發SOAP Web服務必須響應返回下面的示例XML結構:C#如何防止在使用SOAP Web Service時生成二維數組?

<?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對象

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[][]同時產生消費者的二維陣列的陣列?

回答

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; } 
}