Monday, March 4, 2013

XML to XSD to CS Code generator


1) Generate XML with desired tags

<?xml version="1.0"?>
<FLSecurity>
    <MainForm Type="CoreForm" Name="EmployeeDetails.aspx" DisplayName="Employee Details Management">
        <SubForms>
            <SubForm Name="EmployeeDetails.ascx" DisplayName="Employee Details">
                <Controls>
                    <Control Type="DataBound" DisplayName="Business Unit" ID="OrgStructureName" ParentControl="Grid" AssociateControl="lblOrgStructure">                       
                            <UserRoles>
                                <Role ID="1" DisplayName="BC Admin">
                                    <IsVisible>True</IsVisible>
                                    <IsEnabled>True</IsEnabled>
                                </Role>
                            </UserRoles>                       
                    </Control>                   
                </Controls>
            </SubForm>
        </SubForms>
    </MainForm>    
</FLSecurity>
        
2) Generate XSD from XML
xsd file.xml {/classes | /dataset} [/element:element]
             [/language:language] [/namespace:namespace]
3)Create XSD to Code
               http://xsd2code.codeplex.com/



Happy Coding…


public class FLSecurityHelper
    {
        private static FLSecurity m_FLSecurity = null;
        public static FLSecurity FlSecurityXml
        {
            get
            {
                if (m_FLSecurity == null)
                {
                    //TASK: Load xml file form sharepoint list
                    string filepath = @"D:\utilities for testing\Ecm360.FLSecurity\FLSecurity.xml";
                    m_FLSecurity = FLSecurity.LoadFromFile(filepath);
                }
                return m_FLSecurity;
            }
        }


        public static bool IsControlHidden(string formType, string mainForm, string subForm, string fieldID, List&lt;string&gt; userRolesIDs)
        {
            bool isControlHidden = true;
            try
            {
                List&lt;Control&gt; formControls = FlSecurityXml
                                              .Items
                                              .Where(mf =&gt; mf.Type == formType &amp;&amp; mf.Name == mainForm).FirstOrDefault()
                                              .SubForms
                                              .Where(sf =&gt; sf.Name == subForm)
                                              .SelectMany(x=&gt; x.Controls).ToList&lt;Control&gt;();
                                             
                isControlHidden = formControls
                                        .Where(ct =&gt; ct.ID == fieldID)
                                        .ToList()
                                        .FirstOrDefault()
                                        .UserRoles
                                        .Where(rl =&gt; rl.IsVisible == "False"
                                                     &amp;&amp; userRolesIDs.Contains(rl.ID)
                                              ).Count() &gt; 0;
            }
            catch (Exception)
            {
                //throw;
            }
            return isControlHidden;
        }
    }


No comments:

Post a Comment