Main Site Documentation

XML Reading and Writing with SD card


#1

Hello all,

Someone told me the best way to save (and read) user preferences would be to use XML.
Anyway. I understand the working of XML, and I have checked out the samples in the free ebook.

But I have one question left:

Could you provide me a (working) sample of how to write and read XML data with a SD card?

(would be great if you could show me how to change values too, example: user changes name)

Thanks! ;D


#2

Check out my WX reader project. It wasn’t XML, but I do heavy reading and writing to the SD card.
I’m in a bit of a rush now, otherwise I’d extract the method for you. Look in the WX API file. There should be method s to read and write data.


#3

Where is this project, Chris?
Cannot find it on the projects site, neither your blog?


#4

I’m just looking at it now but there is an XML SD project on the NETMF site
(link removed)
its for the domino (I hope its not your project)


#5

I read in configuration information from an SD for my cable tester: (link removed), it might give you some ideas as well.


#6

ian, the project author sais it’s not complete yet, but it might help getting started.

Jeff, your project does not contain all files. It’s missing the following files:

CustomCharecters.cs
ReadXML.cs
TestConfig.cs
UserInterface.cs

Thanks all so far! ;D


#7

Oops! Try it again now. I’m not exactly sure what happened there but CodePlex evidently shifted some things around. I even had trouble trying to connect to the repository as the UUID did not match my local copy. I had to change the UUID in the .svn/entries files in my copy to match what CodePlex now shows. I then did a clean and the missing files were no longer showing as having been added to the repository (but they showed as being added before?)

Anyhow, everything should be there now. (Add this to the list of strangeness from SVN.)


#8

Thanks, the whole project is there now :slight_smile:
Unfortunately I do not understand it completely.

You are referring to testconfig. Could you create a basic setup for me? There is a lot of code in there which I do not need.

It does look very complete though and I will absolutely keep this as a reference for when I am a little more advance with XML.

This is all kind of new to me at this point, so please do not mind that I am not yet understanding this. :slight_smile:


#9

I’d like to suggest an alternative to XML.

Instead of using XML, I normally format everything I can using JSON (at least in NETMF.) It is takes up less space than XML which means you could store more data in memory (both RAM and flash.) It also means you would be sending less data over the wire (Ethernet, XBee, etc.) compared to data formatted in XML.

I created a striped down version of the Json.NEThttp://json.codeplex.com/ library on CodePlex and only used the functionality I really needed.


#10

It looks very promising. What would be the approach with JSON for editing a user name (for example) which is already stored on a SD card?

Would it be more simple? What would it look like? Code example?


#11

I’m not exactly sure what your project looks like, but I’ll throw you a scenario. First though, in case you don’t know what JSON is, take a look at this website http://www.json.org/

  1. You’ll have a file on the SD card called “Users.json”. This file will contain all of your usernames, password, etc, whatever you want.
  2. In your code, you might have a class called “User” that has properties for “username”, “password”, “firstName”, “lastName”, etc.
  3. You can then load the JSON file data into memory and use Json.NET to read all of the objects from the JSON file or even use LINQ to pull out a specific object with a specific username. You can even cast a JSON object to the User class object.
  4. The data in the JSON file might be stored liked {“users”:{“user”:{“username”:“phantomtypist”,“password”:“abc123”}}}

#12

Sounds promising then.
What I basically would like to have is a “config file” with user preferences.

Example:
[ulist]
*Username
*Sound on/off
*Icons placed on window
[/ulist]

Just basic stuff, that’s the idea. But doing this with a text file is very unprofessional.


#13

Fookie, I don’t know if I am misunderstanding your last reply, but this JSON data is just text. The JSON and XML files are just text files. The only difference is the way the data in the file is formatted. XML just happens to take up more space.


#14

I am sure he means plain vs formatted text :wink:


#15

Having “plain text config files” is not unprofessional in the least. In fact, that’s the way some enterprise level web apps I do are. No need to use XML when you just need a DB host, pass and username. All it’s goign to do is add to the amount of stuff the server has to do to parse it.

Not that I’m saying this kind of thing is always appropriate, but it certainly isn’t unprofessional.


#16

“Plain text config files” can be fine.

Foekie, is manually editing these text files the only way you’re letting a “user” update their name ? Perhaps you need a user interface to help deal with that, if you think that’s “not professional”?

In this case, it appears to me that XML might actually be a better choice, as it’s format might be somewhat more “human readable”? I suspect the more obvious delimeters would assist manual editing.


#17

so lets compare a ini style to xml.

<?xml version="1.0" encoding="ISO-8859-1" ?> Tove Jani Reminder "01/01/00

[config]
Firstname=Tove
lastname=Jani
WindowName=Reminder
BirthDay=01/01/00

Both are not great to show your customer and you should provide a interface for it. Not really sure xml is more user readable. Xml has is place as universal data transport. Microsoft likes it for config files instead of ini’s or registry now which is understandable. Again a user should never edit either directly but if you have to i would go with a simple config file instead of something that blows up in your face if they decided to put in a “&” or any other specialty parsed character during manual editing rendering your application config useless.


#18

Both JSON and plaintext are far quicker to parse than XML, no issue for a config file but a consideration if you go further.

IMHO, plaintext is the way to go.


#19

Well, I would to be able to do this with a cobra program…

Ok it seems you guys do not understand me. Let me explain my situation again ;D

Ok, I have a cobra windowed application. There will be some basic configurations. Example:
*sound: value: ON/OFF (for Piëzo sounds)
*username: for basic stuff. For something like “hello, USERNAME”.

Sound on/off will be edited by the cobra program with 2 buttons.
I would like to have the value changed on the sd card then too.

Basically what I want;

A possibility to store some easy data (sound, username) and update it via the cobra program if necessary.

What is the way to go? And please, give me some code examples. ;D
Thanks all so far! ;D


#20

You do not need to read/write a lot of data so the storage space or data transfer rate is of no consequence. All of the tools you need to read and write XML are built into the .NETMF so there is nothing else to add or do (or troubleshoot).

For a simple configuration your going to basically have key/value pairs which you could express as a tag and attribute. In the XML reading for my cable tester the structure is a bit more complicated because each driven connector/pin can be associated with any number of connectors/pins. For reading a configuration you can just do something like:

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