Tural Tural - 2 months ago 16
C# Question

C# How to prevent generating two dimensional array when consuming SOAP Web Service?

We are developing SOAP Web Service which must return the following sample XML structure in response:

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


I've created class structure which do it without problem. The sample structure above is generated right via application. Below I list just part of classes starting from
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; }
}
}


The problem is related with consumer. I've created consumer for this Web Service for testing purposes. It generates the references which converts
Data[] Data
in the
Nested
object to two dimensional array of
Input
. Below is the
Nested
object from generated Reference.cs:

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


How to disable to converting array of
Data
objects
Data[] Data
(which contains the array of
Input
object) to two dimensional array of
Input
objects
Input1[][]
while generating consumer?

Answer

I can find workaround by adding fake field to Data class.

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