C#XML列表反序列化;没有异常抛出,但没有反序列化的对象

时间:2012-03-22 19:04:40

标签: c# xml-deserialization

我正在尝试将XML反序列化为C#对象。当我运行我的代码时,没有抛出任何异常,但我的结果对象是空的。我有点想调试。

我的序列化域类:

[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; }
    }

}

我的反序列化代码:

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");
    }
}

我意识到我的域对象没有反序列化<effective_date><expiration_date>,但我之前忽略了XML元素并且没有问题。我也试过反序列化group_lookup_result并且它成功了;它似乎只是导致问题的group_lookup_result_list对象(或可能是它的List元素)。我的猜测是我没有正确处理命名空间。

所以,我的问题是:

我的序列化配置有什么问题?

我该如何调试?

2 个答案:

答案 0 :(得分:1)

我已经设法复制了这个(很好的例子顺便说一句)并获得反序列化的对象。我将Form = System.Xml.Schema.XmlSchemaForm.Unqualified添加到元素声明中,并且这样做了。我还调整了group_lookup_results属性,以通知反序列化器有关各个项元素名称的信息(使用XmlArrayItemAttribute)。

以下代码。

[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; }
    }

}

答案 1 :(得分:0)

我认为您还需要指定它的XmlArray:

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

XmlArrayXmlArrayItem更具体的示例,其中包含命名空间规范here