|
||
|
|
#1 (permalink) | |
|
Forum Expert
|
Any good books or ebooks, sites, tutorials etc? I'd like to learn the following:
How to save(write) each variable type, how load(read) each variable type.
__________________
Quote:
|
|
|
|
|
|
|
#2 (permalink) |
|
Forum Novice
|
To write and read data in XML and C#, the best way is to design a C# class containing your data and use XmlSerializer to read/write it.
Here a sample from reading and writing a xml preference file: Koders Code Search - MainOptions.cs - C# See the save and load methods. Some very interesting articles about XML and C# can be found on The Code Project - Free Source Code and Tutorials in the XML/XSLT section.
__________________
La connaissance s'accroît quand on la partage. Share your knowledge, you'll increase it. Utopia. Votre Monde. |
|
|
|
|
|
#3 (permalink) |
|
ConnectUO Creator
Join Date: Jan 2004
Age: 27
Posts: 4,824
|
In RunUO: GDK i save and load tons of crap to xml.
Heres an example of how I save Gumps Class Definitions: Gump = Entire Gump BaseGump = Components of a Gump Saving: Gump.cs Code:
public void Save(XmlWriter writer)
{
OnSaveStarted(this, EventArgs.Empty);
writer.WriteStartElement("Gump");
writer.WriteAttributeString("version", GumpVersion.ToString());
writer.WriteAttributeString("resizable", resizable.ToString());
writer.WriteAttributeString("dragable", dragable.ToString());
writer.WriteAttributeString("closable", closable.ToString());
writer.WriteAttributeString("x", x.ToString());
writer.WriteAttributeString("y", y.ToString());
writer.WriteAttributeString("name", name);
foreach (BaseGump bg in Items)
{
writer.WriteStartElement("BaseGump");
bg.Serialize(writer);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.Close();
OnSaveCompleted(this, EventArgs.Empty);
}
Code:
public virtual void Serialize(XmlWriter writer)
{
writer.WriteAttributeString("type", GetType().ToString());
writer.WriteAttributeString("hue", hue.Index.ToString());
writer.WriteAttributeString("index", index.ToString());
writer.WriteAttributeString("z", z.ToString());
writer.WriteAttributeString("x", location.X.ToString());
writer.WriteAttributeString("y", location.Y.ToString());
writer.WriteAttributeString("width", Width.ToString());
writer.WriteAttributeString("height", Height.ToString());
}
Gump.cs Code:
public void Load(XmlDocument doc)
{
OnLoadStarted(this, EventArgs.Empty);
XmlElement root = doc.DocumentElement;// ("Gump");
XmlNodeList baseGumps = root.GetElementsByTagName("BaseGump");
for (int i = baseGumps.Count - 1; i >= 0; i--)
{
Type type = Type.GetType(baseGumps[i].Attributes["type"].Value);
BaseGump gump = (BaseGump)Activator.CreateInstance(type);
string xml = baseGumps[i].OuterXml;
char[] chars = xml.ToCharArray();
byte[] xmlData = Utility.ConvertCharToByteArray(chars);
XmlReader reader = XmlReader.Create(new MemoryStream(xmlData, 0, xmlData.Length));
reader.Read();
gump.Deserialize(reader);
reader.Close();
Items.Add(gump);
}
OnLoadCompleted(this, EventArgs.Empty);
}
Code:
public virtual void Deserialize(XmlReader reader)
{
hue = Hues.GetHue(XmlConvert.ToInt32(reader.GetAttribute("hue")));
index = XmlConvert.ToInt32(reader.GetAttribute("index"));
z = XmlConvert.ToInt32(reader.GetAttribute("z"));
location = new Point(XmlConvert.ToInt32(reader.GetAttribute("x")), XmlConvert.ToInt32(reader.GetAttribute("y")));
size = new Size(XmlConvert.ToInt32(reader.GetAttribute("width")), XmlConvert.ToInt32(reader.GetAttribute("height")));
}
__________________
Jeff Boulanger ConnectUO - Core Developer Want to help make ConnectUO better? Click here to submit your ideas/requests Use your talent to compete against other community members in RunUO hosted coding competitions If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team. Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO |
|
|
|
|
|
#4 (permalink) | |
|
Forum Expert
|
I still don't fully understand this, maybe because I never did any real work with gumps and so it doesn't relate to my previous RUO code...but at any rate, would you mind elaborating? I'd really like to learn this.
__________________
Quote:
|
|
|
|
|
|
|
#5 (permalink) |
|
ConnectUO Creator
Join Date: Jan 2004
Age: 27
Posts: 4,824
|
Best luck would be give me an example of something you are trying to save and Ill show u how I would save it.
__________________
Jeff Boulanger ConnectUO - Core Developer Want to help make ConnectUO better? Click here to submit your ideas/requests Use your talent to compete against other community members in RunUO hosted coding competitions If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team. Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO |
|
|
|
|
|
#6 (permalink) | ||
|
Forum Expert
|
Quote:
So in a class designated for a player: string name; //just an example. Either way, I need to be able to save/load all variable types.
__________________
Quote:
|
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|