
Originally Posted by
DawgiiStylz
That just a lil more advance way of writing xml. More efficient tho true
More advanced or not, this is a small scale project. Imagine yourself reading/writing XML files entry by entry on a file that has more than 100+ fields.
Your code will be huge and ugly. Where as serializing it into a class can take 2-3 lines of code.
Things like serialization are invented and created to remove the need of manually repeating a process over and over.
Here's a quick example (in C# but you should be able to convert it using an online converter easily) showing how simple and elegant serialization is.
Say you have a class like this:
Code:
/**
* Clipper (c) atom0s 2004 - 2013 [wiccaan@comcast.net]
* ---------------------------------------------------------------------------------
* This file is part of Clipper.
*
* Clipper is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Clipper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Clipper. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Clipper.Classes
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
/// <summary>
/// Signature Object Class
///
/// Holds definition information for a single signature.
/// </summary>
public class Signature
{
/// <summary>
/// The name of this signature.
/// </summary>
[XmlAttribute("name")]
public String Name;
/// <summary>
/// The pattern of this signature.
/// </summary>
[XmlAttribute("pattern")]
public String Pattern;
/// <summary>
/// The mask of this signature.
/// </summary>
[XmlAttribute("mask")]
public String Mask;
/// <summary>
/// The offset of this signature.
/// </summary>
[XmlAttribute("offset")]
public Int32 Offset;
}
/// <summary>
/// Offset Object Class
///
/// Holds definition information for a single offset.
/// </summary>
public class Offset
{
/// <summary>
/// The name of this offset.
/// </summary>
[XmlAttribute("name")]
public String Name;
/// <summary>
/// The value of this offset.
/// </summary>
[XmlAttribute("value")]
public Int32 Value;
}
/// <summary>
/// Patch Object Class
///
/// Holds definition information for a single patch.
/// </summary>
public class Patch
{
/// <summary>
/// The name of this patch.
/// </summary>
[XmlAttribute("name")]
public String Name;
/// <summary>
/// The enabled patch code for this patch.
/// </summary>
[XmlAttribute("enabled")]
public String Enabled;
/// <summary>
/// The disabled patch code for this patch.
/// </summary>
[XmlAttribute("disabled")]
public String Disabled;
}
/// <summary>
/// Clipper Configuration Class
///
/// Main configuration class which is deserialized from XML.
/// </summary>
public class Configuration
{
/// <summary>
/// Default Constructor
/// </summary>
public Configuration()
{
this.Signatures = new List<Signature>();
this.Offsets = new List<Offset>();
this.Patches = new List<Patch>();
this.ExcludedPlayers = new List<String>();
this.ExcludedZones = new List<Int32>();
}
/// <summary>
/// Always on top configuration setting.
/// </summary>
public bool AlwaysOnTop { get; set; }
/// <summary>
/// Minimize to tray configuration setting.
/// </summary>
public bool MinimizeToTray { get; set; }
/// <summary>
/// Gets or sets the delay between zoning and reactiving tools.
/// </summary>
public int ZoneDelay { get; set; }
/// <summary>
/// List of signatures from the configuration file.
/// </summary>
public List<Signature> Signatures { get; set; }
/// <summary>
/// List of offsets from the configuration file.
/// </summary>
public List<Offset> Offsets { get; set; }
/// <summary>
/// List of patches from the configuration file.
/// </summary>
public List<Patch> Patches { get; set; }
/// <summary>
/// List of excluded players when checking for detected players.
/// </summary>
[XmlArray]
[XmlArrayItem(ElementName = "Exclude")]
public List<String> ExcludedPlayers { get; set; }
/// <summary>
/// List of excluded zones when checking for detected players.
/// </summary>
[XmlArray]
[XmlArrayItem(ElementName = "Exclude")]
public List<Int32> ExcludedZones { get; set; }
/// <summary>
/// Obtains a signature from the signature list by its name.
/// </summary>
/// <param name="strName"></param>
/// <returns></returns>
public Signature GetSignature(String strName)
{
return (from s in this.Signatures
where s.Name.ToLower() == strName.ToLower()
select s).SingleOrDefault();
}
/// <summary>
/// Obtains an offset from the offset list by its name.
/// </summary>
/// <param name="strName"></param>
/// <returns></returns>
public Offset GetOffset(String strName)
{
return (from s in this.Offsets
where s.Name.ToLower() == strName.ToLower()
select s).SingleOrDefault();
}
/// <summary>
/// Obtains a patch from the offset list by its name.
/// </summary>
/// <param name="strName"></param>
/// <returns></returns>
public Patch GetPatch(String strName)
{
return (from s in this.Patches
where s.Name.ToLower() == strName.ToLower()
select s).SingleOrDefault();
}
}
}
Your configuration file looks like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AlwaysOnTop>false</AlwaysOnTop>
<MinimizeToTray>true</MinimizeToTray>
<ZoneDelay>10</ZoneDelay>
<Signatures>
<Signature name="NPCMAP" pattern="0485FFFFFFFF85C00F84" mask="xx????xxxx" offset="2" />
<Signature name="OWNPOS" pattern="66C7442410790050" mask="xxxxxxxx" offset="37" />
<Signature name="TARGET" pattern="53568BF18B480433DB3BCB75065E33C05B59C38B0D" mask="xxxxxxxxxxxxxxxxxxxxx" offset="45" />
<Signature name="JAWAIT0_1" pattern="8A4424043C0175FF8B81F8FFFFFFFF89" mask="xxxxxxx?xxx????x" offset="14" />
<Signature name="JAWAIT0_2" pattern="FFFFFFFFFFFFFF66C781FFFFFFFF0807C3" mask="???????xxx????xxx" offset="0" />
<Signature name="ZONEID" pattern="8B81FFFFFFFF8B0DFFFFFFFF508B91FFFFFFFF52E8" mask="xx????xx????xxx????xx" offset="8" />
</Signatures>
<Offsets>
<Offset name="PTR_OWNPOS" value="4" />
<Offset name="MOB_NAME" value="124" />
<Offset name="MOB_WARP" value="168" />
<Offset name="MOB_DISTANCE" value="184" />
<Offset name="MOB_TYPE" value="211" />
<Offset name="MOB_RENDER" value="264" />
<Offset name="MOB_FLAG" value="272" />
<Offset name="MOB_SPEED" value="308" />
<Offset name="MOB_STATUS" value="324" />
<Offset name="MOB_SPAWNTYPE" value="412" />
<Offset name="WARP_POSX_1" value="52" />
<Offset name="WARP_POSY_1" value="60" />
<Offset name="WARP_POSZ_1" value="56" />
<Offset name="WARP_POSX_2" value="1472" />
<Offset name="WARP_POSY_2" value="1480" />
<Offset name="WARP_POSZ_2" value="1476" />
</Offsets>
<Patches>
<Patch name="JAWAIT0_1" enabled="90" disabled="40" />
<Patch name="JAWAIT0_2" enabled="90909090909090" disabled="66FF8104010000" />
</Patches>
<ExcludedPlayers />
<ExcludedZones />
</Configuration>
Manually reading each part of this will land up using 50-100 lines of code. Instead, you can serialize the class to and from the xml file with:
Reading / Loading (Deserializing)
Code:
Configuration configObject = null;
using (var reader = new StreamReader(Application.StartupPath + "\\Configuration.xml"))
{
var serializer = new XmlSerializer(typeof(Configuration));
configObject = (Configuration)serializer.Deserialize(reader);
}
Writing / Saving (Serializing)
Code:
using (var writer = new StreamWriter(Application.StartupPath + "\\Configuration.xml"))
{
// Attempt to save the configuration file..
var serializer = new XmlSerializer(typeof(Configuration));
serializer.Serialize(writer, configObject);
}
And that's it. The configObject will contain the data from the file, automatically.