Read/Write data to XML files

17 Jan

[1] DeSerialize data from XML files

XmlSerializer x = new XmlSerializer(typeof(List<MacroCommandCollection>));
using (FileStream fs = new FileStream(FilePath, FileMode.Open))
XmlReader reader = new XmlTextReader(fs);
List<MacroCommandCollection> MacroCmdList = (List<MacroCommandCollection>)x.Deserialize(reader);
return MacroCmdList;

[2] Seserialize data to XML files

XmlSerializer x = new XmlSerializer(MacroCmdList.GetType());
using (FileStream stream = System.IO.File.Create(FilePath))
XmlWriter writer = XmlWriter.Create(stream);
x.Serialize(writer, MacroCmdList);

[3] Read XML files to Dataset or BindingSource object

public static BindingSource LoadCmdFile(string XMLFilePath)
//[1] Read data to a binding source object
DataSet CmdDataSet = new DataSet();
if (CmdDataSet == null)
return null;

//[2] Bind the dataset to the binding source
BindingSource CmdBindingSource = new BindingSource();
CmdBindingSource.DataSource = CmdDataSet;
return CmdBindingSource;

[4] Save Dataset or BindingSource object to XML

public static void SaveCommandSet(BindingSource bs, string XMLCmdSetFile)
DataSet CmdDataSet = bs.DataSource as DataSet;

Leave a comment

Posted by on January 17, 2009 in Dotnet/C#


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: