C # analyze YAML using IronRuby

Source: Internet
Author: User
Tags yaml parser

Yaml is a good format. It is a good choice whether it is used as a configuration file or a small amount of data storage. Ruby works well with it, but the parser in other languages is not complete yet.
C # There is no better Yaml Parser. After searching for nuGet for half a day, I found a parse, but it seems that there is a problem. The yaml file must be indented with two spaces, not a Tab, I am so dizzy.

Later, I thought of a solution to solve this problem. C # Calls are parsed using IronRuby and then returned to C #. IronRuby perfectly implements most of Ruby 1.9. It should be appropriate to use it to analyze Yaml.

Create a new project YLab. Yaml and add references to Ironruby dll. Download ironruby and you will know which references.


Create a folder YLab. Yaml

Create a config. rb File

# Author # Time 2011-6-6 # the Path variable defines the IronRuby SearchPath # The require in the rb file is searched in the SearchPath # The paths here are relative to the application root directory

Path =

[

"RubyLib/ironruby ",

"RubyLib/ruby/1.9.1 ",

"RubyLib/ruby/site_ruby/1.9.1"

]

Def SearchPath

Path

End
 

Create a YLabYAML. rb file.

 

# Author Yu # Time 2011-6-6

Require "yaml"

Class YYAML

Def load (file)

YAML: load_file (file)

End

Def parse (file)

YAML: parse_file (file)

End

End
Then the YAML. cs File

 

 

1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Text;
5
6 using System. IO;
7
8 using Microsoft. Scripting. Hosting;
9 using IronRuby;
10
11 namespace YLab. YAML
12 {
13 public class YAML
14 {
15 private ScriptRuntime irb;
16 private ICollection <string> searchPath = new List <string> ();
17 private dynamic yyaml;
18
19 public YAML ()
20 {
21 irb = Ruby. CreateRuntime ();
22 searchPath. Add (System. IO. Path. Combine (AppDomain. CurrentDomain. BaseDirectory ));
23 searchPath. Add (System. IO. Path. Combine (AppDomain. CurrentDomain. BaseDirectory, @ "YLab. YAML "));
24 irb. GetRubyEngine (). SetSearchPaths (searchPath );
25
26 dynamic rbCfg = irb. UseFile ("config. rb ");
27 var export SearchPath = rbCfg. SearchPath ();
28 foreach (var path in your searchpath)
29 {
30 searchPath. Add (System. IO. Path. Combine (AppDomain. CurrentDomain. BaseDirectory, (string) path ));
31}
32 irb. GetRubyEngine (). SetSearchPaths (searchPath );
33 // ylabYAML = irb. UseFile ("YLabYAML. rb ");
34
35 var assembly = System. Reflection. Assembly. GetExecutingAssembly ();
36
37 string code;
38
39 using (var stream = assembly. GetManifestResourceStream ("YLab. YAML. YLab. YAML. YAML. YLabYAML. rb "))
40 {
41 using (TextReader rbReader = new StreamReader (stream )){
42 code = rbReader. ReadToEnd ();
43}
44}
45 irb. GetRubyEngine (). Execute (code );
46 // irb. GetRubyEngine (). Execute (@"
47 // # author Yu
48 // # Time 2011-6-6
49 // require yaml
50 // class YYAML
51 // def load (file)
52 // YAML: load_file (file)
53 // end
54 //
55 // def parse (file)
56 // YAML: parse_file (file)
57 // end
58 // end
59 //");
60
61 dynamic ruby = irb. Globals;
62 yyaml = ruby. YYAML. @ new ();
63
64}
65
66 public dynamic LoadFile (string yamlFile)
67 {
68 return yyaml. load (yamlFile );
69
70}
71
72 public dynamic ParseFile (string yamlFile)
73 {
74 return yyaml. parse (yamlFile );
75}
76}
77}
In this way, you can


Create another eagleapp

First put a yaml config. ya to be analyzed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.