Element was not expected when deserializing an array with XML serialization

OK I am trying to work with communication with the Pivotal Tracker API, which returns data only in XML format. I have the following XML that I am trying to deserialize in my domain model.


<?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>

The story object is created as follows:


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

When I execute my deserialization code, I get the following exception:


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

Inner Exception:
   <stories xmlns=''> was not expected.

I can deserialize individual stories just fine, I just can't deserialize this xml into an array of history objects

And my deserialization code (value is a string from xml)


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

Does anyone have any ideas?

+3
source share
5 answers

Try something like

public class stories
{
    [XmlElement("story")]
    public story[] storyarray { get; set; }
}

...

var byteArray = Encoding.ASCII.GetBytes(value);
XmlSerializer serializer = new XmlSerializer(typeof(stories));
stories myStories = null;

using (var stream = new MemoryStream(byteArray))
{
    myStories = (stories)serializer.Deserialize(stream);
}

foreach (story stor in myStories.storyarray)
    Console.WriteLine(stor.story_type);

: .

+2

. , :

var deserializedObject = new XmlSerializer(typeof(story[]), new XmlRootAttribute("stories")).Deserialize(stream);

XmlSerializer, . , .

, , , XML, . class story {}, <story>. ( ), XmlType:

[XmlType("story")]
public class Story
{
...
}

, , XML.

+9

, "". XML , stories, .

, , - "":

public class stories : List<story> {}

var byteArray = Encoding.ASCII.GetBytes(value);
stories deserializedObject  = null;
using (var stream = new MemoryStream(byteArray))
{
    var storiesSerializer = new XmlSerializer(typeof (stories));
    deserializedObject = (stories)storiesSerializer .Deserialize(stream);
}
+4

XMSerializer XML, XML.

xmlns="http://schemas.microsoft.com"

... . . XML .

+1

XSD XML, -. , XSD, , , .

  • ( ), XML Visual Studio "XML → ". XSD.
  • To generate classes, run the XSD command from the VS command line. If you run it without parameters, it will show you command line options that you can use.
  • Now you can create a secure XML serializer.
0
source

Source: https://habr.com/ru/post/1738268/


All Articles