// Access by index to elements in document order. // Useful for repeated elements. public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { if (indexes.Length == 1 && indexes[0] is int) { var index = (int)indexes[0]; if (index >= 0) { // this is slow... var el = element_.Elements().Skip(index).FirstOrDefault(); if (el != null) { result = new DynamicXml(el) { Converter = Converter } } ; else { result = new Missing($"{index}. element"); } return(true); } else { // negative index throws exception; // alternatively access from the end of collection can be considered throw new IndexOutOfRangeException(); } } return(base.TryGetIndex(binder, indexes, out result)); }
// Access to XML subnodes or attributes. public override bool TryGetMember(GetMemberBinder binder, out object result) { // is it a subelement? var subelement = Element.Element(binder.Name); if (subelement != null) { result = new DynamicXml(subelement) { Converter = Converter }; return(true); } // if not a subelement, try attributes var attribute = Element.Attribute(binder.Name); if (attribute != null) { result = new DynamicConverter(attribute.Value) { Converter = Converter }; return(true); } // name not found result = new Missing(binder.Name); return(true); }
public ConfigReader(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); //ArgumentException("Missing configuration: " + path); } cfg_ = new DynamicXml(XElement.Load(stream)); }
// Traversal support for node attributes. public static IEnumerable GetAttributes(DynamicXml element, ConfigurationConverterFunc converter) { if (element == null) { return(empty_); } return(element.Element.Attributes().Select( a => new ConfigurationAttribute(a, new DynamicConverter { Value = a.Value, Converter = converter }))); }
public ConfigReader(string xml, bool path) // takes configuration xml file path or xml text { if (xml == null) { throw new ArgumentNullException(nameof(xml)); //ArgumentException("Missing configuration: " + path); } if (path && !File.Exists(xml)) { throw new ArgumentException($"Missing XML file: {path}."); } cfg_ = new DynamicXml(path ? XElement.Load(xml) : XElement.Parse(xml)); }
// Traversal support for nodes. public static IEnumerable GetDescendants(DynamicXml element, bool deep, ConfigurationConverterFunc converter) { if (element == null) { return(empty_); } var elements = deep ? element.Element.Descendants() : element.Element.Elements(); return(elements.Select( d => new ConfigurationElement(d, new DynamicXml(d) { Converter = converter }))); }
public IEnumerable GetAttributes(dynamic element) { return(DynamicXml.GetAttributes(element as DynamicXml, Converter)); }
public IEnumerable GetElements(dynamic element, bool deep) { return(DynamicXml.GetDescendants(element as DynamicXml, deep, Converter)); }