private static void BindArrayToNode(ParsingScope scope, string name, object[] items) { foreach(var container in FindMatchingNodes(scope.Node, name)) { var template = container.SelectSingleNode("./*[@class='template']") ?? container.ChildNodes[0]; ClearTemplateClassFrom(template); template.Remove(); if(items == null || items.Length == 0) continue; for(var x = 0; x < items.Length; x++) { var item = items[x]; var target = template.CloneNode(true); container.ChildNodes.Append(target); var type = item.GetType(); if(IsBindable(type)) target.InnerHtml = (string)item; else ProcessNode(scope.Push(target, item)); } } }
public static string Process(string input, object model) { var document = new HtmlDocument(); document.LoadHtml(input); var scope = new ParsingScope(document.DocumentNode, model); ProcessNode(scope); return document.DocumentNode.WriteTo(); }
private static void ProcessNode(ParsingScope scope) { if(scope.Model == null) return; foreach(var property in scope.Model.GetType().GetProperties( BindingFlags.Public | BindingFlags.Instance)) { var value = property.GetValue(scope.Model, null); if(IsBindable(property.PropertyType)) BindStringToNode(scope.Node, property.Name, value.ToString()); else if(IsArray(property.PropertyType)) BindArrayToNode(scope, property.Name, (object[])value); else { foreach(var target in FindMatchingNodes(scope.Node, property.Name)) ProcessNode(scope.Push(target, value)); } } scope.Pop(); }