Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

XML Deserialization Error

$
0
0

This is the XML file I want to deserialize:

<?xml version="1.0" encoding="utf-8" standalone="yes"?><!--::Sprite Mapping generated by Sprite Vortex::--><AnimationSet><Texture Path="test.png" Name="test.png" /><Animations><Animation Name="yellowBird" FrameRate="55" Loop="True" PingPong="True"><Frames><Frame SpriteId="78" X="6" Y="982" Width="34" Height="24"  Duration="0.01818182" /><Frame SpriteId="79" X="62" Y="982" Width="34" Height="24" Duration="0.01818182" /><Frame SpriteId="80" X="118" Y="982" Width="34" Height="24" Duration="0.01818182" /></Frames></Animation><Animation Name="blueBird" FrameRate="30" Loop="True" PingPong="True"><Frames><Frame SpriteId="81" X="174" Y="982" Width="34" Height="24" Duration="0.01818182" /><Frame SpriteId="59" X="230" Y="658" Width="34" Height="24" Duration="0.01818182" /><Frame SpriteId="62" X="230" Y="710" Width="34" Height="24" Duration="0.01818182" /></Frames></Animation><Animation Name="redBird" FrameRate="29" Loop="True" PingPong="True"><Frames><Frame SpriteId="66" X="230" Y="762" Width="34" Height="24" Duration="0.01818182" /><Frame SpriteId="70" X="230" Y="814" Width="34" Height="24" Duration="0.01818182" /><Frame SpriteId="73" X="230" Y="866" Width="34" Height="24" Duration="0.01818182" /></Frames></Animation></Animations></AnimationSet>

This is the class File for deserialize:

namespace Day004_01
{
    [XmlRoot("AnimationSet")]
    public class AnimationSet
    {
        [XmlElement("Texture", typeof(SpriteTexture))]
        public SpriteTexture Texture;

        [XmlElement("Animations", typeof(Animation))]
        public Animation[] Animations;

        public AnimationSet() { }
    }

    public class Animation
    {
        [XmlElement("Name")]
        public string Name;

        [XmlElement("FrameRate")]
        public int FrameRate;

        [XmlElement("Loop")]
        public bool Loop;

        [XmlElement("PingPong")]
        public bool PingPong;

        [XmlElement("Frames", typeof(Frame))]
        public Frame[] Frames;

        public Animation() { }
    }

    public class Frame
    {
        [XmlAttribute("SpriteId")]
        public int SpriteId;

        [XmlAttribute("X")]
        public int X;

        [XmlAttribute("Y")]
        public int Y;

        [XmlAttribute("Width")]
        public int Width;

        [XmlAttribute("Height")]
        public int Height;

        [XmlAttribute("Duration")]
        public double Duration;

        public Frame() { }
    }

    public class SpriteTexture
    {
        [XmlAttribute("Path")]
        public string Path;

        [XmlAttribute("Name")]
        public string Name;

        public SpriteTexture() { }
    }

    public static class SpriteSheetManager
    {
        public static int AnimationCount;
        public static AnimationSet Read(string filename)
        {
            AnimationSet animationSet = new AnimationSet();

            using(XmlReader reader = XmlReader.Create(filename))
            {
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(AnimationSet));
                    animationSet = (AnimationSet)serializer.Deserialize(reader);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            AnimationCount = animationSet.Animations.Length;
            return animationSet;
        }
    }
}

But I always get a null animationSet.Animation,all properties in Animation (Name,FrameRates,Loop,PingPong...) is null or 0.This question confused me for a long time - -.Who knows how I could fix my program..


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>