thanks Jeff, sorry, but I do not understand this one. Does this mean if it cannot find “On” it’s value will be false?
This is, as far as I can see, the way to go then.
I’ll start creating a XML document then, if someone has suggestions, please please please post them ;D
Come to think about it!
I think it would be up to you to choose.
You did mentioned that you’re going to use buttons to control piezo, right?
since button give you a true/false value, you can go with that too.
Here is the code that I used from the ebook,
I tried both on separate program it working OK.
you should be able to modify to suit your need, very easily, (since you are a Hero.
I do not have any FEZ in my hand right now so, I’m not be able to debug this code myself.
It’s a FEZ Domino code.
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Xml;
using System.Ext.Xml;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
namespace MFConsoleApplication1
{
public class Program
{
public static void Main()
{
#region SD Card reader
// ...
// SD Card is inserted
// Create a new storage device
PersistentStorage sdPS = new PersistentStorage("SD");
// Mount the file system
sdPS.MountFileSystem();
// Assume one storage device is available, acces it through
// Micro Framework and display available files and folders:
// Debug.Print("Getting files and folders: ");
string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
//FileStream FileHandle = new FileStream(rootDirectory + @ "\My Documents\wonderful.txt",
// FileMode.Open, FileAccess.Read);
FileStream FileHandle = new FileStream(rootDirectory + @ "\My Documents\Cobra_UserLogger.xml",
FileMode.Open, FileAccess.Read);
//write the data to file
//
//byte[] data = Encoding.UTF8.GetBytes("This string will go in the file!");
//FileHandle.Write(data, 0, data.Length);
// read data from file
//
byte[] data = new byte[300];
int read_count = FileHandle.Read(data, 0, data.Length);
// close file
FileHandle.Close();
Debug.Print("The Size of the data we read is: " + read_count.ToString());
Debug.Print("Data from file: ");
//Debug.Print(data.ToString());
for (int i = 0; i < read_count; i++)
{
Debug.Print(((char)data[i]).ToString());
}
// if we need to unmount
sdPS.UnmountFileSystem();
////...
//Thread.Sleep(Timeout.Infinite);
#endregion SD Card Reader
#region FEZ XML Read/Write
// FEZ way of writing and reading XML
//
MemoryStream ms = new MemoryStream();
//XML write data to memory
// you should be able to change this
// to write your file to sd Card
XmlWriter xmlwrite = XmlWriter.Create(ms);
xmlwrite.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
// the following tage won't work in NETMF and GHI XML built in driver
//xmlwrite.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"ISO-8859-1\"");
xmlwrite.WriteComment("This is just a comment.");
xmlwrite.WriteRaw("\r\n");
xmlwrite.WriteStartElement("Cobra_UserLogger"); //root element
xmlwrite.WriteString("\r\n\t");
xmlwrite.WriteStartElement("UserName"); //child element
// here where the user name get change before write to file
xmlwrite.WriteString("Foekie");
xmlwrite.WriteEndElement();
xmlwrite.WriteRaw("\r\n\t");
//xmlwrite.WriteStartElement("FileExt");
//xmlwrite.WriteString("txt");
//xmlwrite.WriteEndElement();
//xmlwrite.WriteRaw("\r\n\t");
xmlwrite.WriteStartElement("Sound");
// here where the sound get change between 1 - ON and 0 - OFF
// or you can use any type of value you like
xmlwrite.WriteString("1");
xmlwrite.WriteEndElement();
xmlwrite.WriteRaw("\r\n");
xmlwrite.WriteEndElement(); // end the root element
xmlwrite.Flush();
xmlwrite.Close();
//------ display the XML data ------
byte[] byteArray = ms.ToArray();
char[] cc = System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
string str = new string(cc);
Debug.Print(str);
// Read xml into memory
MemoryStream rms = new MemoryStream(byteArray);
XmlReaderSettings ss = new XmlReaderSettings();
ss.IgnoreWhitespace = true;
ss.IgnoreComments = false;
XmlReader xmlr = XmlReader.Create(rms, ss);
while (!xmlr.EOF)
{
xmlr.Read();
switch (xmlr.NodeType)
{
case XmlNodeType.Element:
Debug.Print("element: " + xmlr.Name);
break;
case XmlNodeType.Text:
Debug.Print("text:" + xmlr.Value);
break;
case XmlNodeType.XmlDeclaration:
Debug.Print("decl: " + xmlr.Name + ", " + xmlr.Value);
break;
case XmlNodeType.Comment:
Debug.Print("comment: " + xmlr.Value);
break;
case XmlNodeType.EndElement:
Debug.Print("end element");
break;
case XmlNodeType.Whitespace:
Debug.Print("white space");
break;
case XmlNodeType.None:
Debug.Print("none");
break;
default:
Debug.Print(xmlr.NodeType.ToString());
break;
}
}
#endregion FEZ XML
}
}
}
// SD Card is inserted
// Create a new storage device
PersistentStorage sdPS = new PersistentStorage("SD");
// Mount the file system
sdPS.MountFileSystem();
// Assume one storage device is available, acces it through
// Micro Framework and display available files and folders:
// Debug.Print("Getting files and folders: ");
string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
FileStream FileHandle = new FileStream(rootDirectory + @ "\XML.xml",
FileMode.Open, FileAccess.Read);
try
{
XmlWriter xmlwrite = XmlWriter.Create(FileHandle);
xmlwrite.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
xmlwrite.WriteComment("This is just a comment.");
xmlwrite.WriteRaw("\r\n");
xmlwrite.WriteStartElement("Cobra_UserLogger"); //root element
xmlwrite.WriteString("\r\n\t");
xmlwrite.WriteStartElement("UserName"); //child element
// here where the user name get change before write to file
xmlwrite.WriteString("Foekie");
xmlwrite.WriteEndElement();
xmlwrite.WriteRaw("\r\n\t");
xmlwrite.WriteStartElement("Sound");
// here where the sound get change between 1 - ON and 0 - OFF
// or you can use any type of value you like
xmlwrite.WriteString("1");
xmlwrite.WriteEndElement();
xmlwrite.WriteRaw("\r\n");
xmlwrite.WriteEndElement(); // end the root element
xmlwrite.Flush();
xmlwrite.Close();
}
catch (Exception e)
{
Debug.Print(e.ToString());
}