Click here to Skip to main content
15,895,746 members
Articles / Web Development / CSS3

AngleSharp

Rate me:
Please Sign up or sign in to vote.
5.00/5 (87 votes)
3 Jul 2013BSD28 min read 269.9K   4.3K   166  
Bringing the DOM to C# with a HTML5/CSS3 parser written in C#.
using AngleSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Samples.ViewModels
{
    public class DOMNodeViewModel : BaseViewModel
    {
        ObservableCollection<DOMNodeViewModel> children;
        String name;
        String typeName;
        String value;
        Boolean selected;
        Boolean expanded;
        Boolean populated;
        Type type;
        Object element;
        DOMNodeViewModel parent;

        public DOMNodeViewModel(Object nodeElement, String nodeName = "document", DOMNodeViewModel nodeParent = null)
        {
            element = nodeElement;
            parent = nodeParent;
            children = new ObservableCollection<DOMNodeViewModel>();
            name = nodeName;

            if (nodeElement == null)
            {
                populated = true;
                typeName = "<null>";
            }
            else if (nodeParent == null)
            {
                CreateChildren();
                IsExpanded = true;
                IsSelected = true;
            }
        }

        public String Name
        {
            get { return name; }
        }

        public String Value
        {
            get { return value; }
            set
            {
                this.value = value;
                RaisePropertyChanged();
            }
        }

        public DOMNodeViewModel Parent
        {
            get { return parent; }
        }

        public String TypeName
        {
            get { return typeName; }
        }

        public ObservableCollection<DOMNodeViewModel> Children
        {
            get { return children; }
        }

        public Boolean IsSelected
        {
            get { return selected; }
            set
            {
                selected = value;
                RaisePropertyChanged();
            }
        }

        public Boolean IsExpanded
        {
            get { return expanded; }
            set
            {
                expanded = value;

                foreach (var child in children)
                    child.CreateChildren();

                RaisePropertyChanged();
            }
        }

        void CreateChildren()
        {
            if (!populated)
            {
                var hv = true;
                populated = true;
                type = element.GetType();
                typeName = FindName(type);

                var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty)
                    .Where(m => m.GetCustomAttributes(typeof(DOMAttribute), false).Length > 0)
                    .OrderBy(m => m.Name);

                foreach (var property in properties)
                {
                    hv = false;

                    switch(property.GetIndexParameters().Length)
                    {
                        case 0:
                            children.Add(new DOMNodeViewModel(property.GetValue(element), FindName(property), this));
                            break;
                        case 1:
                            {
                                if (element is IEnumerable)
                                {
                                    var collection = (IEnumerable)element;
                                    var index = 0;
                                    var idx = new object[1];

                                    foreach (var item in collection)
                                    {
                                        idx[0] = index;
                                        children.Add(new DOMNodeViewModel(item, "[" + index.ToString() + "]", this));
                                        index++;
                                    }
                                }
                            }
                            break;
                    }
                }

                if (hv) Value = element.ToString();
            }
        }

        String FindName(MemberInfo member)
        {
            var objs = member.GetCustomAttributes(typeof(DOMAttribute), false);

            if (objs.Length == 0)
                return member.Name;

            return ((DOMAttribute)objs[0]).OfficialName;
        }
    }
}

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 BSD License


Written By
Chief Technology Officer
Germany Germany
Florian lives in Munich, Germany. He started his programming career with Perl. After programming C/C++ for some years he discovered his favorite programming language C#. He did work at Siemens as a programmer until he decided to study Physics.

During his studies he worked as an IT consultant for various companies. After graduating with a PhD in theoretical particle Physics he is working as a senior technical consultant in the field of home automation and IoT.

Florian has been giving lectures in C#, HTML5 with CSS3 and JavaScript, software design, and other topics. He is regularly giving talks at user groups, conferences, and companies. He is actively contributing to open-source projects. Florian is the maintainer of AngleSharp, a completely managed browser engine.

Comments and Discussions