XML Reading and Writing with SD card

Thanks :smiley:

-1000 experience points! :wink:

lol

Writing is now working fine, thank you all.
Only thing left is reading. I cannot get a working way of how to transform from a memory reader to a actual sd reader.

This is what I have so far, Is this correct, what to change to make it work? Note the comment on where I do not know what to do.

public void ReadXml(string XmlFile)
        {
            // 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;
 
            try
            {
                FileStream
                    FileHandle = new FileStream(rootDirectory + @ "\" + XmlFile + ".xml",
                                                FileMode.Open, FileAccess.Read);

                byte[] data = null; //what to do? 
                char[] cc = System.Text.UTF8Encoding.UTF8.GetChars(data);

                string str = new string(cc);
                Debug.Print(str);

                MemoryStream rms = new MemoryStream(data);

                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;
                    }
                }
                sdPS.UnmountFileSystem();
                sdPS.Dispose();
            }

            catch(Exception e)
            {
                Debug.Print("exception: " + e.ToString());
            }
        }
    }

I think I might overlook some stupid mistake again. Have tried several things, so I just added a “null” to it. Hope you can help me :wink:

Thanks again! :wink:

See what methods are in the FileStream class. One of them is a read function, IIRC.

Tried that one before, then the program hang, the lcd went black and the emx chip was heating, lol.

Will try it again later on, thanks

YES!! I finally got it to work!

I wrote this to a XML file on the SD in the program.cs:


write.WriteXml("XML1", "Foekie", "On");]

Then I read the xml file using the xml reader in the xml read class:

And it returned:


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


<FEZProto_UserData>
	<UserName>Foekie</UserName>
	<Sound>On</Sound>
</FEZProto_UserData>
decl: xml, 
comment All the user data will be stored in this file.
element: FEZProto_UserData
element: UserName
text: Foekie
end element
element: Sound
text: On
end element
end element
none

@ Chris: this is the working solution to reading the file:

byte[] data = new byte[FileHandle.Length];
FileHandle.Read(data, 0, data.Length);
char[] cc = System.Text.UTF8Encoding.UTF8.GetChars(data);

Thank ALL of you! You have been a great help for me! :clap: