Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

YAML Parser in C#

Rate me:
Please Sign up or sign in to vote.
4.83/5 (26 votes)
17 Feb 2011CPOL2 min read 329.8K   4.5K   70  
An almost feature complete YAML parser.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using QiHe.Yaml.Grammar;

namespace QiHe.Yaml.YamlUtility.UI
{
    class YamlEmittor
    {
        public static TreeNode CreateNode(DataItem item)
        {
            if (item is Scalar)
            {
                return CreateNodeForScalar(item as Scalar);
            }
            else if (item is Sequence)
            {
                return CreateNodeForSequence(item as Sequence);
            }
            else if (item is Mapping)
            {
                return CreateNodeForMapping(item as Mapping);
            }
            else
            {
                return null;
            }
        }

        private static TreeNode CreateNodeForScalar(Scalar scalar)
        {
            return new TreeNode(scalar.Text);
        }

        private static TreeNode CreateNodeForSequence(Sequence sequence)
        {
            TreeNode node = new TreeNode("Sequence");
            foreach (DataItem item in sequence.Enties)
            {
                node.Nodes.Add(CreateNode(item));
            }
            return node;
        }

        private static TreeNode CreateNodeForMapping(Mapping mapping)
        {
            TreeNode node = new TreeNode("Mapping");
            foreach (MappingEntry entry in mapping.Enties)
            {
                TreeNode nodeEntry = new TreeNode("Entry");
                nodeEntry.Nodes.Add(CreateNode(entry.Key));
                nodeEntry.Nodes.Add(CreateNode(entry.Value));
                node.Nodes.Add(nodeEntry);
            }
            return node;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect YunCheDa Hangzhou
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions