XML Reading and Writing with SD card

bool soundOn = (reader.GetAttribute("On") == "true")  ? true : false;

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

So this would be valid? (created with XML notepad)


<?xml version="1.0" encoding="utf-8"?>

<FezProtoConfig UserName="Foekie" Sound="On" />

Yeah, but you aren’t really using XML the way it was suppose to be used

You probably want to put the keys and values as children, not as attributes.

<?xml version="1.0" encoding="utf-8"?>

<FezProtoConfig>
  <UserName>Foekie</UserName>
  <Sound>On</Sound>
</FezProtoConfig>

So more like this?
Makes more sense at least :smiley:

Yes, that would work.

You could also do even more generic with just

Slow down you two Fez (super) Hero!
Don’t use your powe too much.

You’re already too fast, resulting cross posting.
You’re even get the answer before you post your question!! :smiley: :smiley:

@ Foekie
I had the same thng for the answer, but I use the number in Sound child instead of text

<Cobra_UserLogger>
Foekie
0
</Cobra_UserLogger>

I cross posted? Where?

Reply #8 and #9

[quote]You could also do even more generic with just
[/quote]

you said this after Foekie post his xml
Not serious just teasing you guys on being Fez hero!! :smiley:

[quote]@ Foekie
I had the same thng for the answer, but I use the number in Sound child instead of text

<Cobra_UserLogger>
Foekie
0
</Cobra_UserLogger>[/quote]

Seems cool to me :slight_smile: I do not know what is more common? Number or text? So 0 or Off?

it could be a bool, true or false.

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.

I believe using bool statements would be the best way then.

Now back to the SD part.
How do I write (and read) XML from and to SD?

See: http://files.chrisseto.com/62W

Thank you Chris, I will have a look at your project! :slight_smile:

It should be in the WX parser file.

Sorry to bother you again, but where is this file?
You have sent me the clock project, is this correct?

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. :smiley:

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
            
        }

    }
}

It should be the in the API folder.

I have been working with Jeff’s code.

I have this now:

            // 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());
            }

But I get a “System.NotSupportedException” on the

XmlWriter xmlwrite = XmlWriter.Create(FileHandle);

What am I doing wrong? (probably something stupid which I keep overlooking at ::slight_smile: )

Thank you!

            FileStream FileHandle = new FileStream(rootDirectory + @ "\XML.xml",
                                                   FileMode.Open, FileAccess.Read);
            try
            {
                XmlWriter xmlwrite = XmlWriter.Create(FileHandle);
 

You have created a file handle with open and read access, but then you try to create a new file to write to with it.