views:

96

answers:

3

Okay so, I have read that .INI files have become obsolete now and the .NET Framework creators want us to use .XML files. However, I feel that it would be difficult for some of my users to use .XML files so I thought of creating a custom config file.

I have a list string which has 3 parameters (it is actually the snippet function in Scintilla), like this:

Snippet.Insert("Name", "Code", 'char');

Now I want to insert all snippets from a files which the user can add himself, including the name, code and character but I have no clue about how I would do this. Maybe something like:

[Snippet1] [Snippet1Code] [Snippet1Char]
[Snippet2] [Snippet2Code] [Snippet2Char]
[Snippet3] [Snippet3Code] [Snippet3Char]

However I don't know how I would read the values that way. If someone can tell me an efficient way to read snippets from a file I would be really grateful.

A: 

Why not just set up your snippets in the xml you talked about then read it with XMLTextReader?

Pretty straight forward...

<snippets>

  <snippet name="Name1" char="a">
    <snippetCode>
      for(int i = 0; i < 10; i++) { // do work }
    </snippetCode>
  </snippet>

  <snippet name="Name2" char="b">
    <snippetCode>
      foreach(var item in items) { // do work }
    </snippetCode>
  </snippet>

</snippets>

C#

public class Snippet
{
    public string Name {get;set;}
    public char Char { get; set;}
    public string Value { get; set;}
}   

List<Snippet> Snippets = new List<Snippet>();

XmlTextReader reader = new XmlTextReader ("snippets.xml");

Snippet snippet = new Snippet();

while (reader.Read()) 
{
    // Do some work here on the data.
    switch (reader.NodeType) 
    {
        case XmlNodeType.Element: 

        if(reader.Name == "snippet"){

           while (reader.MoveToNextAttribute())
           {
              if(reader.Name == "Name"){
                 // get name attribute value
                 snippet.Name = reader.Value;
              }
              else if(reader.Name == "Char"){
                 // get char attribute value
                 snippet.Char = Char.Parse(reader.Value);
              }

           } 
        } 

        break;

        case XmlNodeType.Text: //Display the text in each element.
           //Here we get the actual snippet code
           snippet.Value = reader.Value; 

        break;

        case XmlNodeType. EndElement:

            if(reader.Name == "snippet"){
               // Add snippet to list
               Snippets.Add(snippet);
               // Create a new Snippet object
               snippet = new Snippet();
            }

        break;  

    }
}
gmcalab
Question about this. How would I actually add the snippets to the list when processing the .XML file?
david
@david check my updated answer
gmcalab
Heh, thanks although I did figure out myself. Nevertheless thanks for giving me the idea, really appreciate it ;)
david
A: 

As others have suggested, and similar to @gmcalab's approach, here is a quick example using Linq To XML.

public class Snippet
{
    public string Name { get; set; }
    public string Code { get; set; }
    public char Character { get; set; }
}


XDocument doc = XDocument.Parse(@"<snippets>
                                  <snippet name='snippet1' character='a'> 
                                     <![CDATA[ 
                                       // Code goes here  
                                       ]]>
                                  </snippet>
                                  <snippet name='snippet2' character='b'> 
                                    <![CDATA[ 
                                       // Code goes here
                                        ]]> 
                                  </snippet>
                                </snippets>");

List<Snippet> snippetsList = (from snippets in doc.Descendants("snippet")
                             select new Snippet
                             {
                                 Name = snippets.Attribute("name").Value,
                                 Character = Convert.ToChar(snippets.Attribute("character").Value),
                                 Code = snippets.Value
                             }).ToList();

snippetsList.ForEach(s => Console.WriteLine(s.Code));
Garett
A: 

If you prefer ini files, I've read good things about Nini

ohadsc