Eldorian
Eldorian

Reputation: 613

Trying to Deserialize XML array

I am trying to deserialize an XML document that I am pulling from a 3rd party web service that I have no control over and they only send the data as XML - it looks like the following:

<?xml version="1.0" encoding="utf-8"?>
<GameArray xmlns="http://foo.bar">
  <Game>
    <Id>int</Id>
    <Title>string</Title>
  </Game>
  <Game>
    <Id>int</Id>
    <Title>string</Title>
  </Game>
</GameArray>

I have my class looking like this:

public class GamesModel
    {
        [XmlArray("GameArray")]
        [XmlArrayItem("Game")]
        public List<Game> GameList { get; set; }

        public GamesModel ToXml()
        {
            var request =
                WebRequest.Create(
                    "http://foo.bar/GetGames") as
                HttpWebRequest;

            var xRoot = new XmlRootAttribute
            {
                ElementName = "GameArray",
                Namespace = "http://foo.bar",
                IsNullable = true
            };
            var ser = new XmlSerializer(typeof(GamesModel), xRoot);


            var response = request.GetResponse();
            var result = ser.Deserialize(response.GetResponseStream());


            return (GamesModel)result;
        }
    }

    public class Game
    {
        [XmlElement("Id")]
        public int Id { get; set; }

        [XmlElement("Title")]
        public string Title{ get; set; }

    }

In my controller I have this set up:

[HttpGet]
        public ActionResult GetGames()
        {
            var xboxGames = new GamesModel();

            return Content(GamesModel.ToXml().ToString(), "text/xml");
        }

And then in my view I have some HTML with a button that I want it to call this on the click event - so my jquery looks like this

$('.testTag').click(function() {
    $.ajax({
        type: "GET",
        url: "/Home/GetGames",
        dataType: "xml/text",
        success: function(xml) {
            //dostuff
        }
    });
});

I'm hitting the server just fine on all of this, but I get an error on the line where I desrialize. The error is:

{"There is an error in XML document (2, 2)."}

And the InnerException is:

{"<GameArray xmlns='http://foo.bar/'> was not expected."}

I've been banging my head on this for a couple hours trying to figure out what I need to do.

Upvotes: 3

Views: 611

Answers (1)

L.B
L.B

Reputation: 116158

Your xml serializer needs some help about root element and xml namespace.

XmlSerializer ser = new XmlSerializer(typeof(Game[]), 
              new XmlRootAttribute("GameArray") { Namespace = "http://foo.bar" });
var games = (Game[])ser.Deserialize(stream);

public class Game
{
    public string Id { set; get; }
    public string Title { set; get; }
}

edit

string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<GameArray xmlns=""http://foo.bar"">
    <Game>
    <Id>int</Id>
    <Title>string</Title>
    </Game>
    <Game>
    <Id>int</Id>
    <Title>string</Title>
    </Game>
</GameArray>";

XmlSerializer ser = new XmlSerializer(typeof(Game[]), 
                                        new XmlRootAttribute("GameArray") { Namespace = "http://foo.bar" });
var games = (Game[])ser.Deserialize(new StringReader(xml));

Upvotes: 2

Related Questions