Search code examples
c#asp.netxmlserializationdatacontract

How to deserialize this xml data in c#?


How to serialize this xml using DataContract in c#? The xml data is as follows, soap header and its stuff is handled.

        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:ns1="http://gamespy.net/sake">
        <SOAP-ENV:Body>
            <ns1:GetMyRecords>
                <ns1:gameid>
                    0
                    </ns1:gameid>
                <ns1:secretKey>
                    HA6zkS
                    </ns1:secretKey>
                <ns1:loginTicket>
                    3b7771ac0ae7451a8972d6__
                    </ns1:loginTicket>
                <ns1:tableid>
                    test
                    </ns1:tableid>
                <ns1:fields>
                    <ns1:string>
                        recordid
                        </ns1:string>
                    <ns1:string>
                        ownerid
                        </ns1:string>
                    <ns1:string>
                        MyByte
                        </ns1:string>
                    <ns1:string>
                        MyShort
                        </ns1:string>
                    <ns1:string>
                        MyInt
                        </ns1:string>
                    <ns1:string>
                        MyFloat
                        </ns1:string>
                    <ns1:string>
                        MyAsciiString
                        </ns1:string>
                    <ns1:string>
                        MyUnicodeString
                        </ns1:string>
                    <ns1:string>
                        MyBoolean
                        </ns1:string>
                    <ns1:string>
                        MyDateAndTime
                        </ns1:string>
                    <ns1:string>
                        MyBinaryData
                        </ns1:string>
                    <ns1:string>
                        MyFileID
                        </ns1:string>
                    <ns1:string>
                        num_ratings
                        </ns1:string>
                    <ns1:string>
                        average_rating
                        </ns1:string>
                    </ns1:fields>
                </ns1:GetMyRecords>
            </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>

I already create a base class and it can parse some of data in xml, but the exception will occur when encountered with ns1:fields.

    public class SakeRequestBase
    {
        [DataMember(Name = SakeXmlLable.GameID)]
        public string GameID { get; set; }

        [DataMember(Name = SakeXmlLable.SecretKey)]
        public string SecretKey { get; set; }

        [DataMember(Name = SakeXmlLable.LoginTicket)]
        public string LoginTicket { get; set; }

        [DataMember(Name = SakeXmlLable.TableID)]
        public string TableID { get; set; }

        [DataMember(Name = SakeXmlLable.RecordID)]
        public uint RecordID { get; set; }
    }

The inherited class:

    public class SakeGetMyRecordsRequest : SakeRequestBase
    {
    }

How to write class members for SakeGetMyRecordsRequest to handle the data?


Solution

  • You should try making a class Field that has a single List<string> in it. Then put a property of type Field in your SakeGetMyRecordsRequest. With all the necessary naming attributes, of course.

    You can use tools like https://xmltocsharp.azurewebsites.net/ to get serializable classes for various XMLs. (I'd be wary of posting sensitive info there but it's still useful)