Added support for iterating over DynamicXml
e.g. @foreach(var level in @Model.someXmlProperty.Book)
This commit is contained in:
@@ -5,10 +5,11 @@ using System.Text;
|
||||
using System.Dynamic;
|
||||
using System.Xml.Linq;
|
||||
using System.Xml.XPath;
|
||||
using System.Collections;
|
||||
|
||||
namespace umbraco.MacroEngines
|
||||
{
|
||||
public class DynamicXml : DynamicObject
|
||||
public class DynamicXml : DynamicObject, IEnumerable
|
||||
{
|
||||
public XElement BaseElement { get; set; }
|
||||
|
||||
@@ -16,6 +17,13 @@ namespace umbraco.MacroEngines
|
||||
{
|
||||
this.BaseElement = baseElement;
|
||||
}
|
||||
public string InnerText
|
||||
{
|
||||
get
|
||||
{
|
||||
return BaseElement.Value;
|
||||
}
|
||||
}
|
||||
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
|
||||
{
|
||||
int index = 0;
|
||||
@@ -44,6 +52,7 @@ namespace umbraco.MacroEngines
|
||||
}
|
||||
public override bool TryGetMember(GetMemberBinder binder, out object result)
|
||||
{
|
||||
|
||||
//Go ahead and try to fetch all of the elements matching the member name, and wrap them
|
||||
var elements = BaseElement.Elements(binder.Name);
|
||||
|
||||
@@ -114,5 +123,10 @@ namespace umbraco.MacroEngines
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
return this.BaseElement.Elements().Select(e => new DynamicXml(e)).GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user