0

I am getting an object from a web service call and I am storing that in a hidden field by serializing the object. Then I am deserilizing the object and it is coming like the below screenshot :

enter image description here

My problem is when I am trying to access the below information by many ways, I an getting Invalid cast error OR System.InvalidCastException: Specified cast is not valid

decodedValues[0][2].Value
decodedValues[1][2].Value
decodedValues[2][2].Value

--etc

Any idea how can I get it in ASP.NET 1.1?

James
  • 2,136
  • 3
  • 23
  • 42
  • 2
    How about instead of giving us a screenshot, you give us some code indicating where your data has come from and what form it takes. – Jon Skeet Nov 02 '12 at 07:05
  • @JonSkeet I thought giving scrrenshot would give more information than what I will write..My only need is to fetch those values..and whatever I tried gave me Invalid cast error.. – James Nov 02 '12 at 07:09
  • 1
    Well the invalid cast error itself is something you should have told us about. Code is almost *always* better than a screenshot. (And wow, ASP.NET 1.1? Crikey.) – Jon Skeet Nov 02 '12 at 07:11

1 Answers1

1

A simple method would do the trick:

public string[] GetValues(object[] decodedValues)
{
    string[] returnValues = new string[decodedValues.Length];

    for(int i=0; i<decodedValues.Length; i++)
    {
        returnValues[i] = ((XmlAttribute[])decodedValues[i])[2].Value;
    }

    return returnValues;
}

But remember: If the types from your screenshot don't match, you'll get your InvalidCastException.. So there is room for some improvement to check if the types match.

Matten
  • 17,365
  • 2
  • 42
  • 64
  • Got it..System.InvalidCastException: Specified cast is not valid... Is there something wrong in my object? – James Nov 02 '12 at 07:20
  • When the exception arrises, you should check if the specified member of the decodedValues array is a XmlAttribute[] – Matten Nov 02 '12 at 07:22
  • I have checked..this what it is at runtime.. `decodedValues = System.Object[]` , `decodedValues[0] = System.Xml.XmlAttribute[]` , `decodedValues[0][2] = System.Xml.XmlText` , `decodedValues[0][2].Value = string` – James Nov 02 '12 at 07:29