using System; using System.Collections.Generic; using System.Xml; using Chernobyl.Collections.Generic.Event; using Chernobyl.Reflection.Template.Attribution; using Attribute = Chernobyl.Reflection.Template.Attribution.Attribute; namespace Chernobyl.Reflection.Template.Xml { /// /// An that is generated from or can be written to /// an XML stream. /// public class XmlAttribute : Attribute { /// /// Initializes a new instance of the class. /// /// The instance that /// takes and sets service. /// The XmlReader to read the attribute from. Note /// that, the is expected to be on an XML /// attribute node or an exception will be thrown. /// The /// passed in is not on an XML attribute node. /// Thrown if the /// is not positioned on an /// (see ) of /// . public XmlAttribute(IEventCollection services, XmlReader xmlr) : base(services, string.Empty, string.Empty, string.Empty) { if (xmlr.NodeType != XmlNodeType.Attribute) throw new ArgumentException("Unable to extract the XML attribute data because " + "The XmlReader passed into this constructor is not positioned on an " + "XML attribute. Please ensure the XmlReader is positioned correctly."); Prefix = xmlr.NamespaceURI; Name = xmlr.LocalName; Value = xmlr.Value; } /// /// Creates a collection of instances by /// reading them from an . /// /// The instance that /// takes and sets service. /// The to read the XML /// attributes from. /// The list of s that were read in. public static IList Create(IEventCollection services, XmlReader xmlr) { return Create(services, xmlr, xmlAttribute => { /* no-op */ }); } /// /// Creates a collection of instances by /// reading them from an . /// /// The instance that /// takes and sets service. /// The to read the XML /// attributes from. /// A method that will be invoked for each XML /// attribute read in. This method can be used to process the XML attributes. /// The list of s that were read in. public static IList Create(IEventCollection services, XmlReader xmlr, Action processor) { List attributes = new List(); while (xmlr.MoveToNextAttribute() == true) { XmlAttribute xmlAttribute = new XmlAttribute(services, xmlr); processor(xmlAttribute); attributes.Add(xmlAttribute); } return attributes; } } }