Deserializing a C # XML List; no exception is thrown, but no objects deserialized

I am trying to deserialize XML to C # objects. When I run my code, no exceptions are thrown, but my resulting object is empty. I'm kind of off of debugging ideas.

My serialization domain classes:

[SerializableAttribute()]
[XmlTypeAttribute(TypeName = "group_lookup_result")]
[XmlRootAttribute("group_lookup_result")]
public class GroupLookupResult
{
    private string _groupId;
    private string _groupName;

    public GroupLookupResult()
    {
        _groupId = "";
        _groupName = "";
    }

    [XmlElement("group_id")]
    public string GroupID
    {
        get { return _groupId; }
        set { _groupId = value; }
    }

    [XmlElement("group_name")]
    public string GroupName
    { 
        get { return _groupName; }
        set { _groupName = value; }
    }
}

[SerializableAttribute()]
[XmlRootAttribute(ElementName = "group_lookup_result_list", IsNullable = false, Namespace = "http://ws.byu.edu/namespaces/gro/v1")]
[XmlTypeAttribute(TypeName = "group_lookup_result_list", Namespace = "http://ws.byu.edu/namespaces/gro/v1")]
public class GroupLookupResultList
{
    private List<GroupLookupResult> _searchResults;

    public GroupLookupResultList() { }

    [XmlArrayItem(IsNullable = false)]
    public List<GroupLookupResult> group_lookup_results
    {
        get { return _searchResults; }
        set { _searchResults = value; }
    }

}

      

My deserialization code:

class Program
{
    static void Main(string[] args)
    {
        string incomingXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>" +
            "<ns2:group_lookup_result_list xmlns:ns2=\"http://ws.byu.edu/namespaces/gro/v1\">" + 
                "<group_lookup_results>" + 
                    "<group_lookup_result>" + 
                        "<group_id>bradtest</group_id>" + 
                        "<group_name>Brad Test Group</group_name>" +
                        "<effective_date>2011-05-04T00:00:00-06:00</effective_date>" + 
                        "<expiration_date>2014-01-10T23:59:59-07:00</expiration_date>" + 
                    "</group_lookup_result>" + 
                    "<group_lookup_result>" + 
                        "<group_id>brjtest</group_id>" + 
                        "<group_name>Bernt Test Group</group_name>" + 
                        "<effective_date>2011-05-05T00:00:00-06:00</effective_date>" + 
                        "<expiration_date>2012-05-05T23:59:59-06:00</expiration_date>" + 
                    "</group_lookup_result>" + 
                "</group_lookup_results>" + 
            "</ns2:group_lookup_result_list>";
        XmlSerializer searchResultSerializer = new XmlSerializer(typeof(GroupLookupResultList));
        System.IO.StringReader reader = new System.IO.StringReader(incomingXml);

        GroupLookupResultList resultList = (GroupLookupResultList)searchResultSerializer.Deserialize(reader);
        System.Diagnostics.Debug.WriteLine("Deserialized " + resultList.group_lookup_results.Count + " results");
    }
}

      

I understand that my domain objects do not deserialize <effective_date>

or <expiration_date>

, but I have ignored XML elements so far and had no problem. I also tried to deserialize one group_lookup_result

and it was successful; it seems like it is just an object group_lookup_result_list

(or perhaps an element of it List

) that is causing the problem. I am guessing that I am not handling the namespaces correctly.

So my questions are:

What's wrong with my serialization config?

or

How can I debug this?

+3


source to share


2 answers


I managed to replicate this (good btw example) and get the objects deserialized. I added Form = System.Xml.Schema.XmlSchemaForm.Unqualified to the element declarations and that did the trick. I also slightly modified the group_lookup_results property to tell the deserializer about the individual item names (using the XmlArrayItemAttribute).

The code is below.



[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        string incomingXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>" +
       "<ns2:group_lookup_result_list xmlns:ns2=\"http://ws.byu.edu/namespaces/gro/v1\">" +
           "<group_lookup_results>" +
               "<group_lookup_result>" +
                   "<group_id>bradtest</group_id>" +
                   "<group_name>Brad Test Group</group_name>" +
                   "<effective_date>2011-05-04T00:00:00-06:00</effective_date>" +
                   "<expiration_date>2014-01-10T23:59:59-07:00</expiration_date>" +
               "</group_lookup_result>" +
               "<group_lookup_result>" +
                   "<group_id>brjtest</group_id>" +
                   "<group_name>Bernt Test Group</group_name>" +
                   "<effective_date>2011-05-05T00:00:00-06:00</effective_date>" +
                   "<expiration_date>2012-05-05T23:59:59-06:00</expiration_date>" +
               "</group_lookup_result>" +
           "</group_lookup_results>" +
       "</ns2:group_lookup_result_list>";
        XmlSerializer searchResultSerializer = new XmlSerializer(typeof(GroupLookupResultList));
        System.IO.StringReader reader = new System.IO.StringReader(incomingXml);

        GroupLookupResultList resultList = (GroupLookupResultList)searchResultSerializer.Deserialize(reader);
        System.Diagnostics.Debug.WriteLine("Deserialized " + resultList.group_lookup_results.Count + " results");

    }
}

[SerializableAttribute()]
[XmlTypeAttribute(TypeName = "group_lookup_result")]
[XmlRootAttribute("group_lookup_result")]
public class GroupLookupResult
{
    private string _groupId;
    private string _groupName;

    public GroupLookupResult()
    {
        _groupId = "";
        _groupName = "";
    }

    [XmlElement("group_id", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string GroupID
    {
        get { return _groupId; }
        set { _groupId = value; }
    }

    [XmlElement("group_name", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string GroupName
    {
        get { return _groupName; }
        set { _groupName = value; }
    }
}

[SerializableAttribute()]
[XmlRootAttribute(ElementName = "group_lookup_result_list", IsNullable = false, Namespace = "http://ws.byu.edu/namespaces/gro/v1")]
[XmlTypeAttribute(TypeName = "group_lookup_result_list", Namespace = "http://ws.byu.edu/namespaces/gro/v1")]
public class GroupLookupResultList
{
    private List<GroupLookupResult> _searchResults;

    public GroupLookupResultList() { }

    [System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlArrayItemAttribute("group_lookup_result", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
    public List<GroupLookupResult> group_lookup_results
    {
        get { return _searchResults; }
        set { _searchResults = value; }
    }

}

      

+1


source


I think you need to specify that its XmlArray is also:

[XmlArray("group_lookup_results")]
[XmlArrayItem(IsNullable = false)]
public List<GroupLookupResult> group_lookup_results;

      



There is a more concrete example XmlArray

and XmlArrayItem

with namespace specs here

0


source







All Articles