使用XML序列化反序列化数组时不期望元素

Ant*_*haw 3 c# xml xml-serialization

好.我正在尝试与Pivotal Tracker API进行通信,该API仅返回XML格式的数据.我有以下XML,我正在尝试反序列化到我的域模型中.


<?xml version="1.0" encoding="UTF-8"?>
<stories type="array" count="2" total="2">
  <story>
    <id type="integer">2909137</id>
    <project_id type="integer">68153</project_id>
    <story_type>bug</story_type>
    <url>http://www.pivotaltracker.com/story/show/2909137</url>
    <current_state>unscheduled</current_state>
    <description></description>
    <name>Test #2</name>
    <requested_by>Anthony Shaw</requested_by>
    <created_at type="datetime">2010/03/23 20:05:58 EDT</created_at>
    <updated_at type="datetime">2010/03/23 20:05:58 EDT</updated_at>
  </story>
  <story>
    <id type="integer">2909135</id>
    <project_id type="integer">68153</project_id>
    <story_type>feature</story_type>
    <url>http://www.pivotaltracker.com/story/show/2909135</url>
    <estimate type="integer">-1</estimate>
    <current_state>unscheduled</current_state>
    <description></description>
    <name>Test #1</name>
    <requested_by>Anthony Shaw</requested_by>
    <created_at type="datetime">2010/03/23 20:05:53 EDT</created_at>
    <updated_at type="datetime">2010/03/23 20:05:53 EDT</updated_at>
  </story>
</stories>

Run Code Online (Sandbox Code Playgroud)

我的'故事'对象创建如下:


public class story
{
     public int id { get; set; }
     public int estimate { get; set; }
     public int project_id { get; set; }

        public string story_type { get; set; }
        public string url { get; set; }
        public string current_state { get; set; }
        public string description { get; set; }
        public string name { get; set; }
        public string requested_by { get; set; }
        public string labels { get; set; }
        public string lighthouse_id { get; set; }
        public string lighthouse_url { get; set; }
        public string owned_by { get; set; }
        public string accepted_at { get; set; }
        public string created_at { get; set; }

        public attachment[] attachments { get; set; }
        public note[] notes { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

当我执行我的反序列化代码时,我收到以下异常:


Exception:
   There is an error in XML document (2, 2).

Inner Exception:
   <stories xmlns=''> was not expected.
Run Code Online (Sandbox Code Playgroud)

我可以很好地反序化各个故事,我只是无法将这个xml反序列化为一个'故事'对象数组

我的反序列化代码(值是xml的字符串)


var byteArray = Encoding.ASCII.GetBytes(value);
var stream = new MemoryStream(byteArray);
var deserializedObject = new XmlSerializer(typeof (story[])).Deserialize(stream)
Run Code Online (Sandbox Code Playgroud)

有人有什么想法吗?

luk*_*uke 9

让我提供一个更简洁的解决方案.将反序列化设置为如下所示:

var deserializedObject = new XmlSerializer(typeof(story[]), new XmlRootAttribute("stories")).Deserialize(stream);
Run Code Online (Sandbox Code Playgroud)

通过在XmlSerializer中指定第二个参数,可以避免必须删除该类.它让序列化器知道根元素的名称是什么.

对于这项工作,表示数组元素类型必须完全匹配的XML名称,例如类的名称class story {},<story>.您可以通过指定XmlType来解决这个问题(我建议将其作为最佳实践):

[XmlType("story")]
public class Story
{
...
}
Run Code Online (Sandbox Code Playgroud)

我更喜欢这样做,因为它使我免于被XML类型名称所困扰.