PDA

Click to See Complete Forum and Search --> : [RESOLVED] [2.0] Csharp and XML examples??


birthjay
Feb 19th, 2007, 01:29 PM
Hello,

Using...Visual C# 2005 (Windows forms)

I will be writing an app that required the reading and writing of data in the XML format. I have search this forum and others as well as the net for some good samples, but have come up a bit short. Could someone point me in the right direction? Working samples would be great.

Thanks

Hack
Feb 19th, 2007, 01:35 PM
How about this (http://www.codeproject.com/soap/XMLReadWrite.asp)

Or this (http://www.csharphelp.com/archives/archive199.html)

birthjay
Feb 23rd, 2007, 03:15 PM
Thanks! Good example. Someone said something about saving as XML classes. Do you know what they meant by that?

Thanks again.

ThisIsMyUserName
Feb 25th, 2007, 12:49 AM
If you're just looking to save/load data from your program, this is the class I use. It's pretty straight forward.

FileName: ProgramData.cs

using System;
using System.Text;
using System.Drawing;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace ApplicationStorage
{
public class ProgramData
{
public struct cUserOptions
{
public string userName;
public bool autoSave;
}

public struct cAppSettings
{
public Point windowLocation;
public string pathOpen;
public string pathSave;
}

public cUserOptions UserOptions;
public cAppSettings AppSettings;

#region Constuctor
public Settings()
{
UserOptions.userName = "<Enter Username>";
UserOptions.autoSave = false;

AppSettings.windowLocation.X = 0;
AppSettings.windowLocation.Y = 0;
AppSettings.pathOpen = string.Empty;
AppSettings.pathSave = string.Empty;
}
#endregion

public void Save(ProgramData programData)
{
try
{
using (FileStream fs = new FileStream(SettingsPath, FileMode.Create))
{
XmlSerializer xs = new XmlSerializer(typeof(ProgramData));

xs.Serialize(fs, programData);

fs.Close();
}
}
catch (Exception e)
{
// Your error correction here.
}

}

public Settings Load()
{
ProgramData programData = new ProgramData();

try
{
using (FileStream fs = new FileStream(SettingsPath, FileMode.Open))
{
XmlSerializer xs = new XmlSerializer(typeof(ProgramData));

programData = (ProgramData)xs.Deserialize(fs);

fs.Close();
}
}
catch (Exception e)
{
// Your error correction here.
}

return programData;
}

public static string SettingsPath
{
get
{
StringBuilder path = new StringBuilder();

path.Append(Application.StartupPath);
path.Append(Path.DirectorySeparatorChar);
path.Append(@"settings.xml");

return path.ToString();
}
}
}
}


You can keep your form level code pretty organized (when reading) and you can easily add/remove content and not have to change the underlying code that saves/loads them all.

I'm assuming you'll know how to use the class (create a new instance of the class, run the load method on startup, set your app's properties to whatever you're loading, save method on shutdown, etc..).

I'm not sure if it's the most efficient way possible, but I never had problems.