DevMaster.net Forums
[[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]]

Go Back   DevMaster.net Forums > Site Discussions > Code & Snapshot Discussion
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Closed Thread
 
Thread Tools Search this Thread Display Modes
Old 08-25-2004, 12:07 AM   #1
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

Code:
public class World { public string Name; public Sphere[] Spheres; } public class Sphere { public string Position; }

a simple class structure.. now lets create a world with two spheres

Code:
World w = new World(); w.Name = "MyWorld"; w.Spheres = new Sphere[]{ new Sphere(),new Sphere() }; w.Spheres[0].Position = "Left"; w.Spheres[1].Position = "Right";

and now, we want to save this as xml.. how could we do that?

yes:

Code:
XmlSerializer ser = new XmlSerializer(typeof(World)); XmlTextWriter writer = new XmlTextWriter("World.xml",Encoding.Unicode); writer.Formatting = Formatting.Indented; // we don't want it all in one line, do we? ser.Serialize(writer,w); writer.Close();

and the result? voilą:

Code:
<?xml version="1.0" encoding="utf-16"?> <World xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Name>MyWorld</Name> <Spheres> <Sphere> <Position>Left</Position> </Sphere> <Sphere> <Position>Right</Position> </Sphere> </Spheres> </World>


you want the name and position as xml attribute instead? so add meta-attributes to your classes. like this:

Code:
public class World { [XmlAttribute()] public string Name; public Sphere[] Spheres; } public class Sphere { [XmlAttribute()] public string Position; }

result:

Code:
<?xml version="1.0" encoding="utf-16"?> <World xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="MyWorld"> <Spheres> <Sphere Position="Left" /> <Sphere Position="Right" /> </Spheres> </World>

thats about it..

loading should be rather intuitive now, shouldn't it?

well.. what else do you need? exactly:

Code:
using System.Xml.Serialization; using System.Xml;

and that was it. have fun!
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline  
Old 08-25-2004, 03:10 AM   #2
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

okay.. i've played a bit with inheritance now, too.. here's what i got:


i added a subtype to sphere, namely sun

Code:
public class Sun : Sphere { [XmlAttribute()] public string Shining; }

and changed our second sphere to be a sun:
Code:
World w = new World(); w.Name = "MyWorld"; w.Spheres = new Sphere[]{ new Sphere(),new Sun() }; w.Spheres[0].Position = "Left"; w.Spheres[1].Position = "Right"; ((Sun)w.Spheres[1]).Shining = "Bright";

and got an exception now. they are well documented (right in the exception), so i learned i have to include the type to the xml serializer.. so i did.. like that:

Code:
[XmlInclude(typeof(Sun))] public class Sphere { [XmlAttribute()] public string Position; }

this tells that Sphere will possibly have a subclass of type Sun.

written that attribute, it compiles and runs fine.
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline  
Old 08-25-2004, 06:11 AM   #3
anubis
DevMaster Staff
 
anubis's Avatar
 
Join Date: Apr 2003
Location: Germany
Posts: 2,328
Default

hey... thank you for that !!!
i got the same exception yesterday at 11pm, after we talked about the stuff. i appears the exception doesn't turn up when you write inherited data, so i was confused and postponed the thing. i was to write a code snippet, too. well, maybe if i find out a few other cool things
___________________________________________
If Prolog is the answer, what is the question ?
anubis is offline  
Old 08-25-2004, 06:17 AM   #4
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

i'm currently testing out other ways to collect the information on what inherited class types exist..

one way is to scan the array for it's types, and collect them, and pass as additional parameter to XmlSerializer.. like this:

Code:
static Type[] GetArrayTypes<T>(T[] array) { List<Type> types = new List<Type>(); foreach (T item in array) { Type type = item.GetType(); if (types.Contains(type) == false) { types.Add(type); } } Type[] collected = new Type[types.Count]; for (int i = 0; i < types.Count; i++) { collected[i] = types[i]; } return collected; }

(yeah, i know, a bit long, you can collect directly into an array, too, but you will have doubles/tribles, then.. wich i didn't wanted to).

a next thing, wich could be interesting, is to scan the whole World class, or possibly some type for all findable inherited classes.. with help of the reflection namespace..

if nothing else, i at least learn more about c# and the .NET framework, hehe

oh, and, generics rock!
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline  
Old 02-08-2006, 08:22 AM   #5
oxymoron
New Member
 
Join Date: Feb 2006
Location: The Islamic Republic of England
Posts: 7
Default Re: Saving Data To Xml

What happens if you have a class like a graph?

Code:
public class Node { internal ArrayList _edges; ... } public class Edge { internal Node _nodeA; internal Node _nodeB; ... } public class Graph { private ArrayList _nodes; private ArrayList _edges; }

A node can - and usually is - connected to more than one edge. But AFAICT C# can't tell the difference between a reference and an instance, so If I serialize as illustrated above I get a f**ked up graph. (My custom serialiser converts node references to integer indices and saves these to get around this).
oxymoron is offline  
Old 02-08-2006, 08:58 AM   #6
baldurk
DevMaster Staff
 
baldurk's Avatar
 
Join Date: Jan 2003
Location: Mars
Posts: 1,141
Default Re: Saving Data To Xml

please don't resurrect old posts.
___________________________________________
baldurk
He who knows not and knows that he knows not is ignorant. Teach him.
He who knows not and knows not that he knows not is a fool. Shun him.
baldurk is offline  
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump


All times are GMT -7. The time now is 05:10 AM.


Powered by vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.