3 Replies Latest reply on Oct 7, 2016 4:43 PM by Marcos Vainer Loeff

    how to read all af attributes and values in an element

    Pradeepkumar

      Hi,

       

      I need to read all AF attributes and values from af element.

      Could you please provide me the syntax.

        • Re: how to read all af attributes and values in an element
          pmartin

          Hi Pradeep,

           

          There are multiple ways to do this.  First, you'll need to take a look at the AFAttribute Class

          1) Loop through your element's attributes and GetValue

          foreach(AFAttribute a in example_element.Attributes){
              a.GetValue()
          }
          

          2) Use one of the overloads for FindElementAttributes specified in the AFAttribute class.  The documentation is quite good on these so check them out to find which one best suits your needs.  Once you have the list you can use the AFAttributeList GetValue() to get the values in bulk.  This method is preferable to the first.

          1 of 1 people found this helpful
          • Re: how to read all af attributes and values in an element
            Rick Davin

            Hi Pradeep,

             

            Let's breakdown your request.

            • Stating off, you already have an AFElement.
            • While the element has it's root level attributes via element.Attributes, it sounds like you want to get all attribute generations, that is attribute children, grandchildren, etc.
            • Once you have all attribute generations for that element, you want to GetValue for each attribute.

             

            Did I summarize correctly?  The best tip is that to efficiently issue a GetValue for all attributes, you must use an AFAttributeList.  I have some C# 6.0 static extension methods that can might help.  You would need to place these in a public static class.

             

                    /// <summary>
                    /// Returns a flattened <see cref="AFAttributeList"/> of all generations of attributes for the specified <see cref="AFBaseElement"/>.
                    /// This may be used for bulk data calls which specifically require an <see cref="AFAttributeList"/> rather than generic list or enumerable collection.
                    /// </summary>
                    /// <param name="element">A <see cref="AFBaseElement"/> which could be a <see cref="AFElement"/>, <see cref="AFNotification"/>, or a <see cref="AFEventFrame"/>.</param>
                    /// <returns></returns>
                    public static AFAttributeList GetFlatAttributeList(this AFBaseElement element) => new AFAttributeList(GetAllAttributeGenerations(element.Attributes));
            
            
                    /// <summary>
                    /// Returns a flattened <see cref="IEnumerable<AFAttribute>"/> collection of all generations of all attributes belonging to the specified <see cref="AFBaseElement"/>.
                    /// </summary>
                    /// <param name="element"></param>
                    /// <returns></returns>
                    public static IEnumerable<AFAttribute> GetAllAttributeGenerations(this AFBaseElement element) => GetAllAttributeGenerations(element.Attributes);
            
            
                    /// <summary>
                    /// Returns a flattened <see cref="IEnumerable<AFAttribute>"/> collection of all generations of all attributes including those specified as inputs.
                    /// </summary>
                    /// <param name="attributes"></param>
                    /// <returns></returns>
                    public static IEnumerable<AFAttribute> GetAllAttributeGenerations(this IEnumerable<AFAttribute> attributes)
                    {
                        if (attributes == null)
                            yield break;
                        foreach (AFAttribute attribute in attributes)
                        {
                            yield return attribute;
                            if (attribute.HasChildren)
                            {
                                // Recursion:
                                var descendants = GetAllAttributeGenerations(attribute.Attributes);
                                foreach (AFAttribute descendant in descendants)
                                {
                                    yield return descendant;
                                }
                            }
                        }
                    }
            

             

             

            Again, all you need to do is paste that code into a public static class.  Then you only need 2 lines of code to achieve what you ask.  Let's assume you already have an AFElement in an object named "element".  To get the values for all attributes, and I mean ALL, the code would be:

             

                var attrList = element.GetFlatAttributeList();
                var values = attrList.GetValue();
            

             

            What is returned is an AFValues collection.  You would need to iterate over the collection, but be sure to inspect each AFValue.Attribute property to see which value goes with which attribute.

             

            For more information, see:

            AFAttributeList.GetValue Method

            2 of 2 people found this helpful