0

I am currently using the DataSet.WriteXml method to rely on the solidness of C#'s classes in order to write files.

My problem is that I would like to write the less redundant information possible. By default, XML nodes are written like the following:

<parameter>VALUE</parameter>

But I would rather prefer to write:

<parameter="VALUE" />

Until now, using the MappingType.Attribute I have just been able to format the output like this:

<Table1 parameter="VALUE" />

But, do you have any trick to get rid of "Table1", here ?

EDIT: Or do you have an other class to recommend me to achieve this? I mean, writing my set of keys/values in a proper file...

tshepang
  • 12,111
  • 21
  • 91
  • 136
KwentRell
  • 416
  • 1
  • 4
  • 17
  • 3
    `` isn't valid XML, so that isn't the option you're after. – Rowland Shaw Sep 15 '14 at 08:00
  • What you mean by redundant here..coz cannot be parsed as XML node – Dark Knight Sep 15 '14 at 08:08
  • 1
    @DarkKnight - I think by "redundant" OP means repeating "parameter" in `VALUE`. – Corak Sep 15 '14 at 08:09
  • Yes, it is indeed the repeating of "parameter" which is redundant. – KwentRell Sep 15 '14 at 08:12
  • 1
    Sadly(?) that's how XML works. It's far from being DRY. I think using attributes (as you already do) is the least "redundant" you can get. But attributes always "belong" to an element. If you need this to transfer data, maybe over the web, then JSON might be an alternative for you. – Corak Sep 15 '14 at 08:23
  • Thanks Corak ! I'm going to check JSON. – KwentRell Sep 15 '14 at 08:27
  • 1
    Do you just want to export table data (of one table at a time) into a file? Then maybe CSV could be what you're looking for. Or maybe even writing your own binary format, but then you need to be very careful when using that file between different systems. – Corak Sep 15 '14 at 08:31
  • Yes, writing my table data in a file is my goal, but I would like to rely on a C# or .net writer/reader class rather than on my own binary system. I looked a bit for JSON but haven't found a class such as JSON.WriteFile(... or something like this. – KwentRell Sep 15 '14 at 08:36
  • Maybe this will help (haven't used it myself): http://stackoverflow.com/a/4109886/1336590 – Corak Sep 15 '14 at 08:46
  • I finally used `JavaScriptSerializer` to write my file as JSON, it makes my eyes bleed, but it works... Thanks again Corak! – KwentRell Sep 15 '14 at 09:07

1 Answers1

1

So, In order to avoid redundant data, I switched from XML to JSON, as user Corak suggested me. I ended up with this working code:

    private Dictionary<String, String> settingsConfig = new Dictionary<string,string>();

    private const string JSON_SEPARATOR = "\",\"";
    private const string JSON_SEPARATOR_WITH_NEWLINE = "\",\r\n\"";

    private void readConfig()
    {
        if (File.Exists(defaultFilePath))
        {
            JavaScriptSerializer reader = new JavaScriptSerializer();
            string fileContents = File.ReadAllText(defaultFilePath);
            fileContents = fileContents.Replace(JSON_SEPARATOR_WITH_NEWLINE, JSON_SEPARATOR);
            settingsConfig = reader.Deserialize<Dictionary<String, String>>(fileContents);
        }
    }

    public void saveConfig()
    {
        JavaScriptSerializer writer = new JavaScriptSerializer();
        string textToSave = writer.Serialize(settingsConfig);
        textToSave = textToSave.Replace(JSON_SEPARATOR, JSON_SEPARATOR_WITH_NEWLINE);
        File.WriteAllText(defaultFilePath, textToSave);
    }

And the written file looks like this:

{"parameter0":"VALUE0",
"parameter1":"VALUE1"}
KwentRell
  • 416
  • 1
  • 4
  • 17