Jon
Jon

Reputation: 40062

Deserializing XML to a type with XmlSerializer

I have the below example XML and .NET types with what I think is the correct attributes on the types for the XMLSerializer to use but I just get back empty values in my types. I've tried various attributes in different places but I just can't get the types populated.

[<CLIMutable>]
[<XmlTypeAttribute("ROW")>]
type MyItem =
    { Id: string
      At: string
      Latitude: double
      Longitude: double
      RegNum: string }

[<CLIMutable>]
[<XmlTypeAttribute(AnonymousType = true)>]
type MyRowset =
    { [<XmlArrayAttribute("ROW")>]
      items: MyItem [] }

[<CLIMutable>]
[<XmlTypeAttribute("ROWSET")>]
type Myresult =
    { [<XmlElementAttribute("ROWSET")>]
      rowset: MyRowset }

[<CLIMutable>]
[<XmlTypeAttribute(AnonymousType = true)>]
[<XmlRootAttribute(Namespace = "", IsNullable = false, ElementName = "RESPONSE")>]
type MyResponse =
    { [<XmlElementAttribute("RESULT")>]
      result: Myresult }

    
//<RESPONSE>
//    <RESULT>
//        <ROWSET>
//            <ROW>
//            </ROW>
//        </ROWSET>
//    </RESULT>
//</RESPONSE>

Upvotes: 0

Views: 62

Answers (1)

Vladimir Stoyanov
Vladimir Stoyanov

Reputation: 114

I have updated MyRowset type, now types look like:

<CLIMutable>]
[<XmlTypeAttribute("ROW")>]
type MyItem =
    { 
      Id: string
      At: string
      Latitude: double
      Longitude: double
      RegNum: string }

[<CLIMutable>]
[<XmlTypeAttribute(AnonymousType = true)>]
type MyRowset =
    { [<XmlElement("ROW")>]
      items: MyItem array }

[<CLIMutable>]
[<XmlTypeAttribute("ROWSET")>]
type Myresult =
    { [<XmlElementAttribute("ROWSET")>]
      rowset: MyRowset }

[<CLIMutable>]
[<XmlTypeAttribute(AnonymousType = true)>]
[<XmlRoot(Namespace = "", IsNullable = false, ElementName = "RESPONSE")>]
type MyResponse =
    { [<XmlElementAttribute("RESULT")>]
      result: Myresult }

XML example

<RESPONSE>
    <RESULT>
        <ROWSET>
            <ROW>
                <Id>Id1</Id>
                <At>ATTT</At>
                <Latitude>1.0</Latitude>
                <Longitude>2.0</Longitude>
                <RegNum>test</RegNum>
            </ROW>
        </ROWSET>
    </RESULT>
</RESPONSE>

Output

result:{ result = { rowset = { items = [|{ Id = "Id1"
                                    At = "ATTT"
                                    Latitude = 1.0
                                    Longitude = 2.0
                                    RegNum = "test" }|] } } }

Upvotes: 1

Related Questions