Saving Multiple Fields?

Posts 1–15 of 18 · Page 1 of 2
Saving Multiple Fields?
Hey guys I have seen solutions to this problem such as database's but they seem abit complex im more of a simply type of guy from the picture im hoping your get the idea of what im trying to achieve so once you have made your project you can save it and then choose it in the comx. Even if I could somehow save it into a text file that would be kl because then you could share with other users.



Thanks
Store the data into a class, and serialize the class into a file. You can serialize / deserialize the entire class object easily.
Quote Originally Posted by atom0s View Post
Store the data into a class, and serialize the class into a file. You can serialize / deserialize the entire class object easily.
You know on tuts for this?

Thanks
Quote Originally Posted by atom0s View Post
There's a fairly decent tutorial on Microsofts site:
HOW TO: Serialize and Deserialize XML in Visual Basic .NET
Little to complex for me, was looking at righting an ini or txt file havent been able to get it to work so far. Heres some code im trying with a module I found. So its supposed to read from the .ini file pretty simple so i can understand it but does not seem to fill the text boxs i get no errors just does nothing. Any ideas?

Code:
Dim File = Application.StartupPath + "\Test.ini"
        Dim Section = "Settings"
        Dim FirstName = "FirstName"
        Dim LastName = "LastName"
        TextBox17.Text = ReadIni(File, Section, FirstName, "")
        TextBox16.Text = ReadIni(File, Section, LastName, "")
code for Test.ini File
[Settings]
FirstName=bob
LastName=james
I like .xml's. There's plenty of tuts for that
Quote Originally Posted by DawgiiStylz View Post
I like .xml's. There's plenty of tuts for that
You know any other tuts that I might find abit easier to understand?
Awesome thanks buddy got that in and it works great, I just used this simple method with a comx to load and save the files, but how would i go about deleting a file?

Save
Code:
ComboBox7.Items.Add(ComboBox7.Text)
        If IO.File.Exists(ComboBox7.Text + ".xml") = False Then
            Dim settings As New XmlWriterSettings()
            settings.Indent = True
            Dim XmlWrt As XmlWriter = XmlWriter.Create(ComboBox7.Text + ".xml", settings)
Load
Code:
        If (IO.File.Exists(ComboBox7.Text + ".xml")) Then
            'this is the object that we will loop and will be used to read the xml file
            Dim document As XmlReader = New XmlTextReader(ComboBox7.Text + ".xml")
            While (document.Read())
                Dim type = document.NodeType
Quote Originally Posted by silentrunner2 View Post
Awesome thanks buddy got that in and it works great, I just used this simple method with a comx to load and save the files, but how would i go about deleting a file?

Save
Code:
ComboBox7.Items.Add(ComboBox7.Text)
        If IO.File.Exists(ComboBox7.Text + ".xml") = False Then
            Dim settings As New XmlWriterSettings()
            settings.Indent = True
            Dim XmlWrt As XmlWriter = XmlWriter.Create(ComboBox7.Text + ".xml", settings)
Load
Code:
        If (IO.File.Exists(ComboBox7.Text + ".xml")) Then
            'this is the object that we will loop and will be used to read the xml file
            Dim document As XmlReader = New XmlTextReader(ComboBox7.Text + ".xml")
            While (document.Read())
                Dim type = document.NodeType
Its something like System.I-O.File.Delete(FilePath)
You should really take the time to learn the serialization method. It will make your life a million times easier than reading/writing every single specific value manually.
Quote Originally Posted by atom0s View Post
You should really take the time to learn the serialization method. It will make your life a million times easier than reading/writing every single specific value manually.
I like the xml method it's simple and it works well, coding has always been really difficult for me to learn some people just grasp it. I tend to stick to the simple stuff
Quote Originally Posted by atom0s View Post
You should really take the time to learn the serialization method. It will make your life a million times easier than reading/writing every single specific value manually.
That just a lil more advance way of writing xml. More efficient tho true
Quote Originally Posted by DawgiiStylz View Post

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.
Why in Gods great name would you go to the effort of making xml files when you already have a whole entire part of VS dedicated to exactly what you want?
Just in the same way as you are suggesting to create an xml file VS Project settings does the exact same.

View > [name] Properties > Settings Tab


Code:
My.MySettings.Default.SettingName = Textbox1.Text
No mess no fuss.
Posts 1–15 of 18 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?