1

I have XML in this format -

<Areas>
  <Area>
    <Property Name="Test11">a1</Property>
    <Property Name="Test12">a2</Property>
    <Property Name="Test13">a3</Property>
    <Property Name="Test14">a4</Property>
    <Property Name="Test15">a5</Property>
  </Area>
  <Area>
    <Property Name="Test21">b1</Property>
    <Property Name="Test22">b2</Property>
    <Property Name="Test23">b3</Property>
    <Property Name="Test24">b4</Property>
    <Property Name="Test25">b5</Property>
  </Area>
</Areas>

I generated the class using xsd.exe provided by Microsoft as -

[Serializable()]
    public partial class Areas
    {
        [XmlArrayItem("Property", typeof(AreasAreaProperty))]
        public AreasAreaProperty[][] Area { get; set; }
    }

    [Serializable()]
    public partial class AreasAreaProperty
    {
        [XmlAttribute()]
        public string Name { get; set; }

        [XmlText()]
        public string Value { get; set; }
    }

Code for deserializing is -

private void Deserialize()
        {
            XmlSerializer xs = new XmlSerializer(typeof(Areas));
            FileStream fs = new FileStream("XMLFile1.xml", FileMode.Open);
            XmlReader xr = new XmlTextReader(fs);
            Areas a = (Areas)xs.Deserialize(xr);
            fs.Close();
        }

But at the time of deserilaization, i am getting this error - Cannot convert type 'AreasAreaProperty[]' to 'AreasAreaProperty' I am getting this error at the time of creation of object of XMLSerializer.

How to solve this issue?? Thanks in advance..

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185

3 Answers3

3

I think I've seen this before. XSD.exe isn't perfect and so you need to tinker with the results a little bit. In the following code, on the last line where you have the [][], remove one of the [] so that it's "public AreasAreaProperty[] Area..."

[Serializable()]
public partial class Areas
{
    [XmlArrayItem("Property", typeof(AreasAreaProperty))]
    public AreasAreaProperty[][] Area { get; set; }
mj_
  • 6,297
  • 7
  • 40
  • 80
  • Thanks. The error is not coming anymore but when i check the value of object a. It contains only values for the last Area. How to retrieve the values for the area above? – Rohit Vats Feb 08 '12 at 20:11
  • do you mean something like foreach( AreasAreaProperty a in foobar.Area ) ? – mj_ Feb 09 '12 at 16:38
  • @RohitVats Did you ever figure out how to get both Area's? I'm trying to do the same thing and haven't got it yet. Please post if you solved it. Thanks. – CodeChops Feb 14 '14 at 00:23
1

I've had similar troubles in the past, have a look at the answers to these:

If you have knowledge about your schema you should try and add it to the xsd and not leave everything up to the xsd.exe tool.

Community
  • 1
  • 1
Ian G
  • 29,468
  • 21
  • 78
  • 92
0

Shouldn't the fourth line of your Deserialize() method be

    Areas a = (Areas)xs.Deserialize(xr); 

instead of

    Area a = (Area)xs.Deserialize(xr); 

since your root element is .

cdkMoose
  • 1,646
  • 19
  • 21
  • Sory, that's just a typo. I have corrected it in the question. Actually i am getting this error at the very first line of the method.. – Rohit Vats Feb 08 '12 at 19:56