Ask a questionAsk a question
 

AnswerDeserialization of this XML

  • Monday, November 02, 2009 11:55 PMKen Bagwell Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hello,

    What would be the class definition to deserialize the following XML into ..

    <?xml version="1.0" encoding="UTF-8"?>
    <response xmlns="http://schemas.persony.com/wc2/rest/1.0">
    <sessions start='[START]' next='[NEXT]'>
    <!--BEGIN_SESSIONS-->
    <session in_progress='[IN_PROGRESS]'>
    <session_id>[SESSION_ID]</session_id>
    <host_login>[HOST_LOGIN]</host_login>
    <meeting_id>[MEETING_ID]</meeting_id>
    <meeting_title>[MEETING_TITLE]</meeting_title>
    <start_time>[START_TIME]</start_time>
    <end_time>[END_TIME]</end_time>
    <account_type>[ACCOUNT_TYPE]</account_type>
    <client_data>[CLIENT_DATA]</client_data>
    </session>
    <!--END_SESSIONS-->
    </sessions>
    </response>

    I am trying the following ...

    [XmlRootAttribute("response", Namespace = "http://schemas.persony.com/wc2/rest/1.0")]
    public class sessions
    {
        [XmlElement(ElementName = "session_id")]
        public string session_id { get; set; }

        [XmlElement(ElementName = "host_login")]
        public string host_login { get; set; }

        [XmlElement(ElementName = "meeting_id")]
        public string meeting_id { get; set; }

        [XmlElement(ElementName = "meeting_title")]
        public string meeting_title { get; set; }

        [XmlElement(ElementName = "start_time")]
        public string start_time { get; set; }

        [XmlElement(ElementName = "end_time")]
        public string end_time { get; set; }

        [XmlElement(ElementName = "max_concurrent")]
        public string max_concurrent { get; set; }

        [XmlElement(ElementName = "account_type")]
        public string account_type { get; set; }

        [XmlElement(ElementName = "client_data")]
        public string client_data { get; set; }

    }

    And the following returns an object with null properties

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                try
                {
                    XmlSerializer ser = new XmlSerializer(typeof(sessions));
                    return (sessions)ser.Deserialize(response.GetResponseStream());
                }
                catch(Exception e)
                {
                    string err;
                    err =  e.InnerException.ToString();
                    return null;
                }

    Thanks, Ken

Answers

  • Tuesday, November 03, 2009 2:25 AMRaymond Tang Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,ken,you should define some more classes

    [XmlRoot(''response")]
    [Serializable]
    public class MyResponse
    {
    [XmlElement("sessions")]
    public Sessions Sessions;
    }

    [Serializable]
    public class  Sessions
    {
    [XmlAttribute("start")]
    public string Start;
    [XmlAttribute("next")]
    public string Next;

    [XmlElement("session",Type=typeof(session))]
    public session Session;
    }

    [Serializable]
    public class session
    {
        [XmlAttribute("in_progress")]
        public string in_progress;
        [XmlElement(ElementName = "session_id")]
        public string session_id { get; set; }

        [XmlElement(ElementName = "host_login")]
        public string host_login { get; set; }

        [XmlElement(ElementName = "meeting_id")]
        public string meeting_id { get; set; }

        [XmlElement(ElementName = "meeting_title")]
        public string meeting_title { get; set; }

        [XmlElement(ElementName = "start_time")]
        public string start_time { get; set; }

        [XmlElement(ElementName = "end_time")]
        public string end_time { get; set; }

        [XmlElement(ElementName = "max_concurrent")]
        public string max_concurrent { get; set; }

        [XmlElement(ElementName = "account_type")]
        public string account_type { get; set; }

        [XmlElement(ElementName = "client_data")]
        public string client_data { get; set; }

    }

    Then deserialize as follow:

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                try
                {
                    XmlSerializer ser = new XmlSerializer(typeof(MyResponse));
                    return (MyResponse)ser.Deserialize(response.GetResponseStream());
                }
                catch(Exception e)
                {
                    string err;
                    err =  e.InnerException.ToString();
                    return null;
                }

    if sessions element is a collection of session,you should make some change.

    Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~
  • Wednesday, November 04, 2009 4:02 AMRaymond Tang Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,Ken
    if it is a collection of session entity,then you should make some change as follow:

    [Serializable]
    public class  Sessions
    {
    [XmlAttribute("start")]
    public string Start;
    [XmlAttribute("next")]
    public string Next;

     [XmlArray("mySessions")]
     [XmlArrayItem("session", Type = typeof(session))]

    public session[] Session;
    }


    now the xml should changed to


    <sessions start='[START]' next='[NEXT]'>

    <mySessions>
    <!--BEGIN_SESSIONS-->

    <session in_progress='[IN_PROGRESS]'>
    <session_id>[SESSION_ID]</session_id>

    <host_login>[HOST_LOGIN]</host_login>

    <meeting_id>[MEETING_ID]</meeting_id>

    <meeting_title>[MEETING_TITLE]</meeting_title>

    <start_time>[START_TIME]</start_time>

    <end_time>[END_TIME]</end_time>

    <account_type>[ACCOUNT_TYPE]</account_type>

    <client_data>[CLIENT_DATA]</client_data>

    </session>

    <!--END_SESSIONS-->
    </mySessions>

     </sessions>


    if you want to keep the current xml structure,you should make an effort.

    you should implement IXmlSerializable interface.
    here are some examples:
    http://www.codeproject.com/KB/XML/yaxlib.aspx#h3formatcol

    http://www.codeproject.com/KB/XML/IXmlSerializable.aspx?display=Print



    Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~

All Replies

  • Tuesday, November 03, 2009 2:25 AMRaymond Tang Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,ken,you should define some more classes

    [XmlRoot(''response")]
    [Serializable]
    public class MyResponse
    {
    [XmlElement("sessions")]
    public Sessions Sessions;
    }

    [Serializable]
    public class  Sessions
    {
    [XmlAttribute("start")]
    public string Start;
    [XmlAttribute("next")]
    public string Next;

    [XmlElement("session",Type=typeof(session))]
    public session Session;
    }

    [Serializable]
    public class session
    {
        [XmlAttribute("in_progress")]
        public string in_progress;
        [XmlElement(ElementName = "session_id")]
        public string session_id { get; set; }

        [XmlElement(ElementName = "host_login")]
        public string host_login { get; set; }

        [XmlElement(ElementName = "meeting_id")]
        public string meeting_id { get; set; }

        [XmlElement(ElementName = "meeting_title")]
        public string meeting_title { get; set; }

        [XmlElement(ElementName = "start_time")]
        public string start_time { get; set; }

        [XmlElement(ElementName = "end_time")]
        public string end_time { get; set; }

        [XmlElement(ElementName = "max_concurrent")]
        public string max_concurrent { get; set; }

        [XmlElement(ElementName = "account_type")]
        public string account_type { get; set; }

        [XmlElement(ElementName = "client_data")]
        public string client_data { get; set; }

    }

    Then deserialize as follow:

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                try
                {
                    XmlSerializer ser = new XmlSerializer(typeof(MyResponse));
                    return (MyResponse)ser.Deserialize(response.GetResponseStream());
                }
                catch(Exception e)
                {
                    string err;
                    err =  e.InnerException.ToString();
                    return null;
                }

    if sessions element is a collection of session,you should make some change.

    Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~
  • Tuesday, November 03, 2009 9:44 PMKen Bagwell Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Raymond, that helps.
    The sessions element is a collection of sessions.
    I have modified the sessions class:

    [Serializable]
    public class sessions
    {
        [XmlAttribute("start")]
        public string Start;
        [XmlAttribute("next")]
        public string Next;

        [XmlElement("session", Type = typeof(List<session>))]
        public List<session> Session;
    }

    But the list does not populate. Any ideas?

    Thanks,
    Ken
  • Wednesday, November 04, 2009 4:02 AMRaymond Tang Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,Ken
    if it is a collection of session entity,then you should make some change as follow:

    [Serializable]
    public class  Sessions
    {
    [XmlAttribute("start")]
    public string Start;
    [XmlAttribute("next")]
    public string Next;

     [XmlArray("mySessions")]
     [XmlArrayItem("session", Type = typeof(session))]

    public session[] Session;
    }


    now the xml should changed to


    <sessions start='[START]' next='[NEXT]'>

    <mySessions>
    <!--BEGIN_SESSIONS-->

    <session in_progress='[IN_PROGRESS]'>
    <session_id>[SESSION_ID]</session_id>

    <host_login>[HOST_LOGIN]</host_login>

    <meeting_id>[MEETING_ID]</meeting_id>

    <meeting_title>[MEETING_TITLE]</meeting_title>

    <start_time>[START_TIME]</start_time>

    <end_time>[END_TIME]</end_time>

    <account_type>[ACCOUNT_TYPE]</account_type>

    <client_data>[CLIENT_DATA]</client_data>

    </session>

    <!--END_SESSIONS-->
    </mySessions>

     </sessions>


    if you want to keep the current xml structure,you should make an effort.

    you should implement IXmlSerializable interface.
    here are some examples:
    http://www.codeproject.com/KB/XML/yaxlib.aspx#h3formatcol

    http://www.codeproject.com/KB/XML/IXmlSerializable.aspx?display=Print



    Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~