开发者

C# Reading from a custom file

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开发者_JAVA百科 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.


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));


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


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;  

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜