0
Answer

Problem in serializing response

Rohit S

Rohit S

15y
2.1k
1
Hi all
I am developing web service of costumes.regarding this i am using Business class where classes are defined.
Public class ProductType
{
      [XmlAttribute]
      public string pname;
      public string[] size;
}
[XmlRoot("GetProducts")]
Public class GetProductsResponse
{
    [XmlElement("Products")]
    public ProductType[] productlist
}
----------------------------------------------------------------------

I want the following output:
<GetProduct>
<Products pname="ABC">
<size>L</size>
<size>m</size>
<size>s</size>
</Product>
......

Now the problem is I am getting following output:
<GetProduct>
<Product pname="ABC">
<Product>
<size>L</size>
</Product>
<Product>
<size>m</size>
</Product>
<Product>
<size>s</size>
</Product>

--------------------------------------------


Mainly stored procedure is run at the backend which will fetch values into the dataset at the frontend C#.
In the dataset there are two columns Pname and Size.
I have tried for loops but unable to get the right output.

Fragment of code
int icount=0;
GetProductResponse prd=new Getprouctresponse();
prd.product=new productType[ds.Tables[0].Rows.Count];
for(in i=0;i<ds.Tables[0].Rows.Count;i++)
{
currentproduct=ds.tables[0].rows[i]["Pname"].tostring();
if(currentproduct!=previousproduct)
{
  prd.product[iCount]=new producttype();//new array
  prd.product[iCount].pname=currentproduct;
  int32 k=0;
  prd.product[iCount].size[k]=ds.tables[0].rows[i]["size"].tostring();
  iCount=iCount+1;
 
}
else
{
  k=k+1;
  prd.product[iCount].size[k]=ds.tables[0].rows[i]["size"].tostring();
  iCount=ICount+1;

}

The Else part above, generates Null exception. If i add  prd.product[iCount]=new producttype();in the else part, then i am getting the output which i dont need.


Please rectify my problem. It seems to be problem of initialization of size array. Please help me out asap.

Thanks