public override bool TryGetMember(GetMemberBinder binder, out object result) { // Setup initial conditions. result = null; // Handle the Value and Count special cases. switch (binder.Name) { case Value: result = xmlElements[0].Value; break; case Count: result = xmlElements.Count; break; default: { // Try to find a named attribute first. var attr = xmlElements[0].Attribute(XName.Get(binder.Name)); if (attr != null) { // If a named attribute was found, return that NON-dynamic object. result = attr; } else { // Find the named descendants. var items = xmlElements.Descendants(XName.Get(binder.Name)); if (items != null && items.Count() > 0) { // Prepare a new dynamic object with the list of found descendants. result = new DynamicXml(items); } } } break; } if (result == null) { // Element not found, create a new element here. xmlElements[0].AddFirst(new XElement(binder.Name)); result = new DynamicXml(xmlElements[0].Descendants().First()); } // Finish up. return true; }
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { var firstIndex = (int)indexes[0]; result = new DynamicXml(xmlElements[firstIndex]); return true; }