Skip to main content
gamedev.net
🔒 Locked

[C#] XML - Class Structure

Started by Pox 2 replies 800+ views
Original Post
Pox
Pox
Hey all :) I have had a bit of a search around for this, but I haven't started developing anything yet, for the hope that there's an easier way. I'm using C#/Mono on Linux, but Mono is an almost full implementation of .NET, so suggest based on .NET. Don't ask why I'm using this platform: basically, it's the lesser of 4 evils that are valid platforms for a competition I'm entering. Basically what I want to do is read an XML element into an instance of a class, basically to get something like this:

<Particle>
    <Lifetime>100</Lifetime>
    <Sprite>smoke1.png</Sprite>
    <DeltaSpeedMin>1</DeltaSpeedMin>
    <DeltaSpeedMax>2</DeltaSpeedMax>
    <DeltaAngleMin>0</DeltaAngleMin>
    <DeltaAngleMax>0</DeltaAngleMax>
    <InitialAlpha>1</InitialAlpha>
    <DeltaAlphaMin>0</DeltaAlphaMin>
    <DeltaAlphaMax>0</DeltaAlphaMax>
</Particle>
into this:
public class Particle {
        // these will be set at runtime, not constants
        public double x;
        public double y;
        public double Speed;
        public double Angle;
        int Ticks;

        // these will be read from XML
        int Lifetime;
        String Sprite;

        double DeltaSpeedMin;
        double DeltaSpeedMax;
        double DeltaAngleMin;
        double DeltaAngleMax;

        double InitialAlpha; // 0..1
        double DeltaAlphaMin;
        double DeltaAlphaMax;
}
Now, the obvious solution would be to use the .NET XML parser to get an XML node tree structure, then do manual assignments for each instance - but that seems like a poor solution, with every member added needing changes to the parser as well. I come from a perl background, so I would normally use an associative array rather than a fixed structure, which makes this kind of thing easy as anything - but for obvious reasons, that's much too slow when I'm processing so many particles per frame. I'm _hoping_ there's an easy way to do this, probably facilitated through the fact that .NET is bytecode and not fully compiled - does anyone have any tips?
LordShade
LordShade
Look at the [Serialized] keyword in C#. It does what you want.

Happy coding.
Pox
Pox
Quote:
Original post by LordShade
Look at the [Serialized] keyword in C#. It does what you want.

Happy coding.


:D I'm having a quick look at the docos now, looks perfect for my needs :)

I'll post back if I have any major problems. Thanks for the incredibly swift reply.
Mike.Popoloski
Mike.Popoloski
The .NET framework provides a serialization framework for things like this. Basically, you mark the elements of your class with attributes like XmlRoot, XmlElement, etc. Then, you use the XmlSerializer to do it all automatically.

Here is an example. Also, you will obviously want to check out MSDN on XmlSerializer.
Mike Popoloski | Journal | SlimDX

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.