Retrieving element values from a custom config section with nested collection

Divya Arunraj 1 Reputation point
2021-02-23T12:24:23.91+00:00

Hi,

I need help on retrieving element values of the following custom Section in the configuration .

<SecurityAttributeCustomMetadata>
<attribute displayName="Security : projectname">
<add collectionTypeName="BasicExperiment" sectionTypeName="Title" fieldName="Metadata" propertyName="ProjectName"/>
</attribute>
<attribute displayName="Security : projectNumber">
<add collectionTypeName="BasicExperiment" sectionTypeName="Title" fieldName="Metadata" propertyName="ProjectNumber"/>
</attribute>
<!-- Fields from two section type can be combined together and configure as a single security attribute in the SNB -->
<attribute displayName="Security : customName">
<add collectionTypeName="BasicExperiment" sectionTypeName="Title" fieldName="Metadata" propertyName="userName"/>
<add collectionTypeName="MyExperimentType" sectionTypeName="MySectionType" fieldName="MyFieldName" propertyName="MyPropertyName"/>
</attribute>

Please suggest.

Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,586 Reputation points
    2021-02-23T13:25:06.483+00:00

    @Divya Arunraj ,

    By using LINQ to XML it is easy to achieve what you need.

    I saved your XML as a file on the file system.

    <?xml version="1.0"?>  
    <SecurityAttributeCustomMetadata>  
     <attribute displayName="Security : projectname">  
     <add collectionTypeName="BasicExperiment" sectionTypeName="Title"  
          fieldName="Metadata" propertyName="ProjectName"/>  
     </attribute>  
     <attribute displayName="Security : projectNumber">  
     <add collectionTypeName="BasicExperiment" sectionTypeName="Title"  
          fieldName="Metadata" propertyName="ProjectNumber"/>  
     </attribute>  
     <!-- Fields from two section type can be combined together and configure as a single security attribute in the SNB -->  
     <attribute displayName="Security : customName">  
     <add collectionTypeName="BasicExperiment" sectionTypeName="Title"  
          fieldName="Metadata" propertyName="userName"/>  
     <add collectionTypeName="MyExperimentType"  
          sectionTypeName="MySectionType" fieldName="MyFieldName"  
          propertyName="MyPropertyName"/>  
     </attribute>  
    </SecurityAttributeCustomMetadata>  
    

    c#

    void Main()  
    {  
     XDocument xdoc = XDocument.Load(@"e:\Temp\CustomConfigSection.xml");  
      
     foreach (XElement xelem in xdoc.Descendants("add"))  
     {  
     Console.WriteLine("collectionTypeName: '{0}', sectionTypeName: '{1}'"  
     , xelem.Attribute("collectionTypeName").Value  
     , xelem.Attribute("sectionTypeName").Value  
     );  
     }  
    }  
    

    Output

    collectionTypeName: 'BasicExperiment', sectionTypeName: 'Title'  
    collectionTypeName: 'BasicExperiment', sectionTypeName: 'Title'  
    collectionTypeName: 'BasicExperiment', sectionTypeName: 'Title'  
    collectionTypeName: 'MyExperimentType', sectionTypeName: 'MySectionType'  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.