Results 1 to 9 of 9

Thread: xml settings files into C# plugin

  1. #1
    Raw Wave pierrotm777's Avatar
    Join Date
    May 2008
    Location
    Bordeaux, France
    Posts
    2,856

    xml settings files into C# plugin

    I try to convert a VB.net class into a C# class but i have an error !

    VB.NET class:
    Code:
    Option Strict Off
    Option Explicit On
    
    Imports System.IO
    Imports System.Xml
    Imports System.Xml.Serialization
    Imports System.Text
    
    Public Class cMySettings
    
        Public FTPUsername As String
        Public FTPPassword As String
        Public FTPHostname As String
        Public FTPHostpath As String
        Public FTPDirectorypath As String
    
        Private Shared XMLFilename As String
    
        Public Sub New()
            SetToDefault(Me)
        End Sub
    
        Public Sub New(FileName As String)
            XMLFilename = FileName
            If Path.GetExtension(XMLFilename).ToLower() <> "xml" Then XMLFilename = XMLFilename + ".xml"
            If File.Exists(XMLFilename) = False Then SerializeToXML(New cMySettings())
        End Sub
    
        Public Sub New(ByRef Settings As cMySettings)
            Me.FTPUsername = Settings.FTPUsername
            Me.FTPPassword = Settings.FTPPassword
            Me.FTPHostname = Settings.FTPHostname
            Me.FTPHostpath = Settings.FTPHostpath
            Me.FTPDirectorypath = Settings.FTPDirectorypath
        End Sub
    
        Public Shared Sub SerializeToXML(ByRef Settings As cMySettings)
            Dim xmlSerializer As New XmlSerializer(GetType(cMySettings))
            Using xmlTextWriter As New XmlTextWriter(XMLFilename, Encoding.UTF8)
                xmlTextWriter.Formatting = Formatting.Indented
                xmlSerializer.Serialize(xmlTextWriter, Settings)
                xmlTextWriter.Close()
            End Using
        End Sub
    
        Public Shared Sub DeseralizeFromXML(ByRef Settings As cMySettings)
            Dim fs As FileStream = Nothing
            ' do i have settings?
            If File.Exists(XMLFilename) = True Then
                Try
                    fs = New FileStream(XMLFilename, FileMode.Open, FileAccess.Read)
                    Dim xmlSerializer As New XmlSerializer(GetType(cMySettings))
                    Settings = xmlSerializer.Deserialize(fs)
                Catch
                    'load error of some sort, or OBJECT deserialize error
                    'do we tell anyone?
                    Exit Sub
                Finally
                    If Not fs Is Nothing Then fs.Close()
                    fs = Nothing
                End Try
            End If
    
        End Sub
    
        Public Shared Sub Copy(ByRef SourceSettings As cMySettings, ByRef DestSettings As cMySettings)
            DestSettings.FTPUsername = SourceSettings.FTPUsername
            DestSettings.FTPPassword = SourceSettings.FTPPassword
            DestSettings.FTPHostname = SourceSettings.FTPHostname
            DestSettings.FTPHostpath = SourceSettings.FTPHostpath
            DestSettings.FTPDirectorypath = SourceSettings.FTPDirectorypath
        End Sub
    
        Public Shared Sub SetToDefault(ByRef Settings)
            Settings.FTPUsername = "FTPUsername"
            Settings.FTPPassword = "FTPPassword"
            Settings.FTPHostname = "FTPHostname"
            Settings.FTPHostpath = "Disque dur/Enregistrements/"
            Settings.FTPDirectorypath = "C:\temp\FTPCopy\"
        End Sub
    
        Public Shared Function Compare(ByRef Settings1 As cMySettings, ByRef Setting2 As cMySettings) As Boolean
    
            If Settings1.FTPUsername <> Setting2.FTPUsername Then Compare = False : Exit Function
            If Settings1.FTPPassword <> Setting2.FTPPassword Then Compare = False : Exit Function
            If Settings1.FTPHostname <> Setting2.FTPHostname Then Compare = False : Exit Function
            If Settings1.FTPHostpath <> Setting2.FTPHostpath Then Compare = False : Exit Function
            If Settings1.FTPDirectorypath <> Setting2.FTPDirectorypath Then Compare = False : Exit Function
            Compare = True
    
        End Function
    
    End Class
    C# class converted:
    Code:
    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Text;
    
    public class cMySettings
    {
    
    	public string FTPUsername;
    	public string FTPPassword;
    	public string FTPHostname;
    	public string FTPHostpath;
        	public string FTPDirectorypath;
    
    	private static string XMLFilename;
    	public cMySettings()
    	{
    		SetToDefault(ref this);
    	}
    
    	public cMySettings(string FileName)
    	{
    		XMLFilename = FileName;
    		if (Path.GetExtension(XMLFilename).ToLower() != "xml")
    			XMLFilename = XMLFilename + ".xml";
    		if (File.Exists(XMLFilename) == false)
    			SerializeToXML(ref new cMySettings());
    	}
    
            public cMySettings(ref cMySettings Settings)
    	{
    		this.FTPUsername = Settings.FTPUsername;
    		this.FTPPassword = Settings.FTPPassword;
    		this.FTPHostname = Settings.FTPHostname;
    		this.FTPHostpath = Settings.FTPHostpath;
    		this.FTPDirectorypath = Settings.FTPDirectorypath;
    	}
    
    	public static void SerializeToXML(ref cMySettings Settings)
    	{
    		XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings));
    		using (XmlTextWriter xmlTextWriter = new XmlTextWriter(XMLFilename, Encoding.UTF8)) {
    			xmlTextWriter.Formatting = Formatting.Indented;
    			xmlSerializer.Serialize(xmlTextWriter, Settings);
    			xmlTextWriter.Close();
    		}
    	}
    
    	public static void DeseralizeFromXML(ref cMySettings Settings)
    	{
    		FileStream fs = null;
    		// do i have settings?
    		if (File.Exists(XMLFilename) == true) {
    			try {
    				fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read);
    				XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings));
    				Settings = xmlSerializer.Deserialize(fs);
    			} catch {
    				//load error of some sort, or OBJECT deserialize error
    				//do we tell anyone?
    				return;
    			} finally {
    				if ((fs != null))
    					fs.Close();
    				fs = null;
    			}
    		}
    
    	}
    
    	public static void Copy(ref cMySettings SourceSettings, ref cMySettings DestSettings)
    	{
    		DestSettings.FTPUsername = SourceSettings.FTPUsername;
    		DestSettings.FTPPassword = SourceSettings.FTPPassword;
    		DestSettings.FTPHostname = SourceSettings.FTPHostname;
    		DestSettings.FTPHostpath = SourceSettings.FTPHostpath;
    		DestSettings.FTPDirectorypath = SourceSettings.FTPDirectorypath;
    	}
    
    	public static void SetToDefault(ref Settings)
    	{
    		Settings.FTPUsername = "FTPUsername";
    		Settings.FTPPassword = "FTPPassword";
    		Settings.FTPHostname = "FTPHostname";
    		Settings.FTPHostpath = "Disque dur/Enregistrements/";
    		Settings.FTPDirectorypath = "C:\\temp\\FTPCopy\\";
    	}
    
    	public static bool Compare(ref cMySettings Settings1, ref cMySettings Setting2)
    	{
    		bool functionReturnValue = false;
    
    		if (Settings1.FTPUsername != Setting2.FTPUsername){functionReturnValue = false;return functionReturnValue;}
    		if (Settings1.FTPPassword != Setting2.FTPPassword){functionReturnValue = false;return functionReturnValue;}
    		if (Settings1.FTPHostname != Setting2.FTPHostname){functionReturnValue = false;return functionReturnValue;}
    		if (Settings1.FTPHostpath != Setting2.FTPHostpath){functionReturnValue = false;return functionReturnValue;}
    		if (Settings1.FTPDirectorypath != Setting2.FTPDirectorypath){functionReturnValue = false;return functionReturnValue;}
    		functionReturnValue = true;
    		return functionReturnValue;
    
    	}
    
    }
    The SetToDefault function return me an error !

    Where is my error please !

    Thanks

  2. #2
    Variable Bitrate
    Join Date
    Jul 2010
    Posts
    321
    What's the actual error you are getting? I thought you could only set static variables inside a static method, but I might be wrong...
    Current Worklog: TBA - '05 Rav4 (Mobo Dead).
    www.rav4world.com - Forums for Rav4 owners
    www.flavorfeasts.com - Cooking website/forums
    -Basic forums live atm, come contribute!

  3. #3
    Raw Wave pierrotm777's Avatar
    Join Date
    May 2008
    Location
    Bordeaux, France
    Posts
    2,856
    Quote Originally Posted by detlion1643 View Post
    What's the actual error you are getting? I thought you could only set static variables inside a static method, but I might be wrong...
    In french the error is 'Identificateur attendu'

  4. #4
    Raw Wave pierrotm777's Avatar
    Join Date
    May 2008
    Location
    Bordeaux, France
    Posts
    2,856
    I have found my issue with the msdn help !!!

    the good translated class is :
    Code:
    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Text;
    
    public class cMySettings
    {
    
    	public string FTPUsername;
    	public string FTPPassword;
    	public string FTPHostname;
    	public string FTPHostpath;
        public string FTPDirectorypath;
    
    	private static string XMLFilename;
    
        public cMySettings()
        {
            SetToDefault(this);
        }
    
    	public cMySettings(string FileName)
    	{
    		XMLFilename = FileName;
    		if (Path.GetExtension(XMLFilename).ToLower() != "xml")
    			XMLFilename = XMLFilename + ".xml";
            if (File.Exists(XMLFilename) == false)
                SerializeToXML(new cMySettings());
    	}
    
    	public cMySettings(cMySettings Settings)
    	{
    		this.FTPUsername = Settings.FTPUsername;
    		this.FTPPassword = Settings.FTPPassword;
    		this.FTPHostname = Settings.FTPHostname;
    		this.FTPHostpath = Settings.FTPHostpath;
    		this.FTPDirectorypath = Settings.FTPDirectorypath;
    	}
    
    	public static void SerializeToXML(cMySettings Settings)
    	{
    		XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings));
    		using (XmlTextWriter xmlTextWriter = new XmlTextWriter(XMLFilename, Encoding.UTF8)) {
    			xmlTextWriter.Formatting = Formatting.Indented;
    			xmlSerializer.Serialize(xmlTextWriter, Settings);
    			xmlTextWriter.Close();
    		}
    	}
    
    	public static void DeseralizeFromXML(cMySettings Settings)
    	{
    		FileStream fs = null;
    		// do i have settings?
    		if (File.Exists(XMLFilename) == true) {
    			try {
    				fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read);
    				XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings));
                    XmlReader reader = XmlReader.Create(fs);
                    cMySettings i;
                    i = (cMySettings)xmlSerializer.Deserialize(reader);
    			} catch {
    				//load error of some sort, or OBJECT deserialize error
    				//do we tell anyone?
    				return;
    			} finally {
    				if ((fs != null))
    					fs.Close();
    				fs = null;
    			}
    		}
    
    	}
    
    	public static void Copy(cMySettings SourceSettings,cMySettings DestSettings)
    	{
    		DestSettings.FTPUsername = SourceSettings.FTPUsername;
    		DestSettings.FTPPassword = SourceSettings.FTPPassword;
    		DestSettings.FTPHostname = SourceSettings.FTPHostname;
    		DestSettings.FTPHostpath = SourceSettings.FTPHostpath;
    		DestSettings.FTPDirectorypath = SourceSettings.FTPDirectorypath;
    	}
    
        public static void SetToDefault(cMySettings Settings)
        {
            Settings.FTPUsername = "FTPUsername";
            Settings.FTPPassword = "FTPPassword";
            Settings.FTPHostname = "FTPHostname";
            Settings.FTPHostpath = "Disque dur/Enregistrements/";
            Settings.FTPDirectorypath = "C:\\temp\\FTPCopy\\";
        }
    
    	public static bool Compare(cMySettings Settings1,cMySettings Setting2)
    	{
    		bool functionReturnValue = false;
    
    		if (Settings1.FTPUsername != Setting2.FTPUsername){functionReturnValue = false;return functionReturnValue;}
    		if (Settings1.FTPPassword != Setting2.FTPPassword){functionReturnValue = false;return functionReturnValue;}
    		if (Settings1.FTPHostname != Setting2.FTPHostname){functionReturnValue = false;return functionReturnValue;}
    		if (Settings1.FTPHostpath != Setting2.FTPHostpath){functionReturnValue = false;return functionReturnValue;}
    		if (Settings1.FTPDirectorypath != Setting2.FTPDirectorypath){functionReturnValue = false;return functionReturnValue;}
    		functionReturnValue = true;
    		return functionReturnValue;
    
    	}
    
    }
    http://msdn.microsoft.com/fr-fr/libr.../d15aekdk.aspx
    and
    http://msdn.microsoft.com/fr-fr/libr.../he66c7f1.aspx

  5. #5
    Raw Wave pierrotm777's Avatar
    Join Date
    May 2008
    Location
    Bordeaux, France
    Posts
    2,856
    I have always an issue with the deserialer function!
    I have tried:
    Code:
        public static void DeseralizeFromXML(cMySettings Settings)
        {
            FileStream fs = null;
            // do i have settings?
            if (File.Exists(XMLFilename) == true)
            {
                try
                {
                    fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read);
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings));
                    Settings = (cMySettings)xmlSerializer.Deserialize(fs);
                }
                catch
                {
                    //load error of some sort, or OBJECT deserialize error
                    //do we tell anyone?
                    return;
                }
                finally
                {
                    if ((fs != null))
                        fs.Close();
                    fs = null;
                }
            }
    
        }
    and also:
    Code:
        public static void DeseralizeFromXML(cMySettings i)
        {
            FileStream fs = null;
            // do i have settings?
            if (File.Exists(XMLFilename) == true)
            {
                try
                {
                    fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read);
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings));
                    XmlReader reader = XmlReader.Create(fs);
                    //cMySettings i;
                    i = (cMySettings)xmlSerializer.Deserialize(reader);
                }
                catch
                {
                    //load error of some sort, or OBJECT deserialize error
                    //do we tell anyone?
                    return;
                }
                finally
                {
                    if ((fs != null))
                        fs.Close();
                    fs = null;
                }
            }
    
        }
    But when i try to read my variable , by example PluginSettings.COMPort , the variable is empty !

  6. #6
    Raw Wave pierrotm777's Avatar
    Join Date
    May 2008
    Location
    Bordeaux, France
    Posts
    2,856
    Ok, If i convert the VB.NET code that follow , i obtain an error for the red line !!!
    Code:
        Public Shared Sub DeseralizeFromXML(ByRef Settings As cMySettings)
            Dim fs As FileStream = Nothing
            ' do i have settings?
            If File.Exists(XMLFilename) = True Then
                Try
                    fs = New FileStream(XMLFilename, FileMode.Open, FileAccess.Read)
                    Dim xmlSerializer As New XmlSerializer(GetType(cMySettings))
                    Settings = xmlSerializer.Deserialize(fs)
                Catch
                    'load error of some sort, or OBJECT deserialize error
                    'do we tell anyone?
                    Exit Sub
                Finally
                    If Not fs Is Nothing Then fs.Close()
                    fs = Nothing
                End Try
            End If
    
        End Sub
    In c# i obtain these lines:
    Code:
        public static void DeseralizeFromXML(cMySettings Settings)
        {
            FileStream fs = null;
            // do i have settings?
            if (File.Exists(XMLFilename) == true)
            {
                try
                {
                    fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read);
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings));
                    Settings = xmlSerializer.Deserialize(fs);
                }
                catch
                {
                    //load error of some sort, or OBJECT deserialize error
                    //do we tell anyone?
                    return;
                }
                finally
                {
                    if ((fs != null))
                        fs.Close();
                    fs = null;
                }
            }
    
        }
    the error is 'can't convert the object in cMySettings

    Thanks for your help !!!

  7. #7
    MySQL Error
    Auto Apps:loading...

    Join Date
    Oct 2004
    Posts
    5,259
    not seeing this completely

    but in c# classes that you want to serialize should have a Serializable attribute

    [Serializable]

    then serial/deserial is easy!
    that code looks like my code

    anyway, there is tons of C# examples outside of this forum...

    attached is example

    hey I got some C# books, can you read English well enough?, id give you a book for free, just pay shipping
    good books.... ill get you more info... and titles
    (yea, real books, made of paper!)
    Attached Files Attached Files
    -Thanks
    Mitch
    www.rush2112.net

    "Did you test it in carwings??"

    Sun, Come shine my way
    May healing waters bury all my pain
    Wind, Carry me home
    The fabric of reality is tearing apart
    The piece of me that died
    Will return To live again

  8. #8
    Raw Wave pierrotm777's Avatar
    Join Date
    May 2008
    Location
    Bordeaux, France
    Posts
    2,856
    Quote Originally Posted by mitchjs View Post
    hey I got some C# books, can you read English well enough?, id give you a book for free, just pay shipping
    good books.... ill get you more info... and titles
    (yea, real books, made of paper!)
    Why not, Thanks !

  9. #9
    Raw Wave pierrotm777's Avatar
    Join Date
    May 2008
    Location
    Bordeaux, France
    Posts
    2,856
    micthjs, you are the best :-)
    Into my Deserialize command , i have just added a 'ref' i don't know why and all is running well know so my code is :
    Code:
        public static void DeseralizeFromXML(ref cMySettings Settings)
        {
            FileStream fs = null;
            // do i have settings?
            if (File.Exists(XMLFilename) == true)
            {
                try
                {
                    fs = new FileStream(XMLFilename, FileMode.Open, FileAccess.Read);
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(cMySettings));
                    //cMySettings i;
                    Settings = (cMySettings)xmlSerializer.Deserialize(fs);
                }
                catch
                {
                    //load error of some sort, or OBJECT deserialize error
                    //do we tell anyone?
                    return;
                }
                finally
                {
                    if ((fs != null))
                        fs.Close();
                    fs = null;
                }
            }
    
        }
    And i initialise the xml file like that:
    Code:
       DPath = pluginDataPath;
    
                //Settings with Xml file
                PluginSettings = new cMySettings(DPath + "iCarDuino");
    
                // read in defaults
                cMySettings.DeseralizeFromXML(ref PluginSettings);
                // copy to temp
                TempPluginSettings = new cMySettings(PluginSettings);

Similar Threads

  1. Csv2GpsExec -Files convertissor for the GPSexec plugin
    By pierrotm777 in forum Software & Software Development
    Replies: 22
    Last Post: 10-19-2011, 11:40 AM
  2. Replies: 24
    Last Post: 02-01-2009, 08:06 AM
  3. Chucks simple plugin settings menu
    By Chuck in forum Road Runner
    Replies: 10
    Last Post: 01-21-2009, 12:05 PM
  4. Export definitions + .h files for SD plugin dlls
    By marchello2000 in forum DigitalMods (Scripts / API)
    Replies: 5
    Last Post: 07-01-2007, 04:50 PM
  5. Markus Zhender Plugin Config Files
    By bombboyer in forum LCD/Display
    Replies: 3
    Last Post: 11-06-2002, 03:07 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •