3

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

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's 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's 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 realize that my domain objects don't deserialize the <effective_date> or <expiration_date>, but I've ignored XML elements before and didn't have a problem. I've also tried deserializing a single group_lookup_result and it was successful; it seems that it's just the group_lookup_result_list object (or possibly its List element) that is causing problems. My guess is that I'm not handling the namespaces correctly.

So, my questions are:

What is wrong with my serialization configuration?

or

How can I debug this?

0

2 Answers 2

1

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

Code 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's 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's 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; }
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

It worked; thanks. I haven't learned about the Form parameters yet. I think that'll be my next reading subject.
@RustyTheBoyRobot yeah, the Form stuff stung me a bit when I first started doing this. I didn't look into the whys after I had found the solution but probably should in retrospect. Another thing that stung often was the not decorating the the list properties properly by making sure had a name for individual elements etc good luck with your solutions
0

I think you need to specify that its an XmlArray as well:

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

There is a more specific example of XmlArray and XmlArrayItem with namespace specifications here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.