7 Replies - 1779 Views - Last Post: 25 March 2010 - 03:24 PM Rate Topic: -----

#1 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Access resource XML file

Posted 25 March 2010 - 12:54 PM

Say I have some presets for an application that won't ever be changed. I want to store it within the .exe itself by using it as a resource. Or say I just add the file to the project. How would I go about in my code accessing it? Currently I have this for my external file:

        string pFilename = "presets.dat";
        XmlDocument PresetFile = new XmlDocument();
        List<string> PresetNames = new List<string>(); //collection containing names of all presets

        public List<string> GeneratePresetList()
        {
            try
            {
                if (File.Exists(pFilename))
                {
                    PresetFile.Load(pFilename); //if preset file exists load it

                    XmlNode root = PresetFile.DocumentElement; //get document root element

                    if (root is XmlElement)
                    {
                        //get a list of all elements named preset
                        XmlNodeList pNodeList = PresetFile.SelectNodes("/data/preset");

                        foreach (XmlNode pNode in pNodeList)
                        {
                            //get the name attribute of each preset
                            XmlAttribute pName = pNode.Attributes["name"];

                            //add name of preset to the Presetnames collection
                            PresetNames.Add(pName.Value);
                        }

                        return PresetNames; //return the collection after it is populated
                    }
                }
            }
            catch(Exception ex)
            {
                Prompts.DisplayError(ex);
            }

            return null;
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Access resource XML file

#2 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Access resource XML file

Posted 25 March 2010 - 01:41 PM

You know, I have lately been reviewing the source code of the ZedGraph library (http://zedgraph.org/wiki/index.php?title=Main_Page) and they do something I find to be quite elegant in the application. Any area that has preset configurations, they created a nested structure called Default.

So, in the constructor, they end up setting everything up for the instance by something like this:

class Widgit
{
    // Consumer code can set these properties whenever.
    public string Name { get; set; }
    public decimal Price { get; set; }

    // Nested structure of static values to provide default configuration.
    public struct Default
    {
        public const string Name = "Unknown";
        public const decimal Price = 0.0M;
    }


    // Blank constructor should use default presets for an unknown widgit.
    public Widgit()
    {
        Name = Default.Name;
        Price = Default.Price;
    }

    // Consumer code can alternately specify values which doesn't use defaults any longer.
    public Widgit(string name, decimal price)
    {
        Name = name;
        Price = price;
    }
}



I personally like this idea. The default presets are all encapsulated in the object itself. There's no outside configuration files to tamper with and since every field is constant then a modification can't take place unless it's a source edit of the default configuration.

So, you might consider using this technique if the values never change (or you know a default value). Even if they will, there's a very minimal change here.

Hope this helps.

EDIT: Originally had kinda screwy code.

EDIT2: Updated code again.
As for the actual ZedGraph implementation, they're using static fields but I think const makes way more sense for what this is so I did it that way here.

This post has been edited by MentalFloss: 25 March 2010 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Re: Access resource XML file

Posted 25 March 2010 - 02:06 PM

My XML file basically holds various presets for the user to pick. They can then apply the changes which writes certain values to the registry. So since I don't know what they will pick I need to give them options. So I am not sure if I understand how your code would work for this application. Its not my program presets but rather preset values I need to work with later.
Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Access resource XML file

Posted 25 March 2010 - 02:32 PM

Then what you really meant to say when you said:

Quote

Say I have some presets for an application that won't ever be changed.


was "I have some presets that are configurable by a user and can be changed to anything at any given time"


I would have answered differently.


In the case of application wide configuration pieces, you should add an app.config to your project. Within it, create keys and assign values.
Do you know how to work with an app.config file? If not, say so and I'll fill in the details about it for you.
Was This Post Helpful? 0
  • +
  • -

#5 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Re: Access resource XML file

Posted 25 March 2010 - 03:01 PM

The presets are for an external applications. The second application has a list formed from registry keys. Now my program lets the user pick what they want to add to the list. It then writes the values to the registry which then updates the list of program 2. Nothing in program 1 is ever changed, and the items that can be added to program 2's list are part of the XML file program 1 reads.
Was This Post Helpful? 0
  • +
  • -

#6 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Access resource XML file

Posted 25 March 2010 - 03:13 PM

Quote

The presets are for an external applications. The second application has a list formed from registry keys. Now my program lets the user pick what they want to add to the list. It then writes the values to the registry which then updates the list of program 2. Nothing in program 1 is ever changed, and the items that can be added to program 2's list are part of the XML file program 1 reads.


You're going to want to research how to serialize and deserialze XML content then.

Your user will load the configuration portion (data in file is deserialized into an object)
User then makes modification and saves (object is modified with changes and then serialized to file)
Was This Post Helpful? 0
  • +
  • -

#7 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Re: Access resource XML file

Posted 25 March 2010 - 03:16 PM

If I add my XML as a resource in the solution can I just change the filename to be Resources.filename instead?
Was This Post Helpful? 0
  • +
  • -

#8 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Access resource XML file

Posted 25 March 2010 - 03:24 PM

It looks like this might help you out:
http://cf-bill.blogs...d-resource.html

Or if it doesn't, do a simple google search for C# XML resources. That should get you plenty to work with.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1