none
How to extract Azman Roles and Users into an Excel sheet using C#.net or VB Macro RRS feed

  • Question

  • Hi,

    Can someone please suggest me how to extract the users added in a specific group on Authorization Manager file. I would like to extract the details of Azman ( Roles Assigned , Users part of the group and related details ) in an excel file. I tried many options but could not find any specific information about how to do this. 

    Azman sample file structure somewhat looks like below. 

    <AzApplicationGroup Guid="c2658b0e-9231-4a57-b56c-aba8ac8cd430" Name="Everyone" Description="" GroupType="Basic">
    <Member>S-1-1-0</Member>
    </AzApplicationGroup>
    <AzApplicationGroup Guid="a2298919-7d20-4c0c-ae5f-4e72df27f7c0" Name="MesAdministrators" Description="" GroupType="Basic">
    <Member>S-1-5-21-1266324296-1186871245-1398891620-30322</Member>
    <Member>S-1-5-21-1266324296-1186871245-1398891620-35715</Member>
    <Member>S-1-5-21-8915387-1821125878-1660491571-13146</Member>
    <Member>S-1-5-21-2734516016-2858594118-375011225-10599</Member><Member>S-1-5-21-2734516016-2858594118-375011225-6713</Member><Member>S-1-5-21-2734516016-2858594118-375011225-25279</Member><Member>S-1-5-21-2734516016-2858594118-375011225-11294</Member><Member>S-1-5-21-2734516016-2858594118-375011225-11492</Member><Member>S-1-5-21-2734516016-2858594118-375011225-47923</Member><Member>S-1-5-21-2734516016-2858594118-375011225-25358</Member></AzApplicationGroup>
    <AzApplicationGroup Guid="21c6148f-5365-4c94-9baa-96422e7c8e1c" Name="CarBakingFurnaceOperator" Description="(production) " GroupType="Basic">
    <Member>S-1-5-21-1266324296-1186871245-1398891620-9081</Member>
    </AzApplicationGroup>
    <AzApplicationGroup Guid="c8f4536f-4028-430d-bcb9-4e1bac1255f3" Name="CarPastePlantOperator" Description="(production) " GroupType="Basic">
    <Member>S-1-5-21-1266324296-1186871245-1398891620-9080</Member>
    </AzApplicationGroup>
    <AzApplicationGroup Guid="38d04e06-8df7-414e-95e7-8873717c9575" Name="CarBakingFurnaceSupervisor" Description="(production) " GroupType="Basic">
    <Member>S-1-5-21-1266324296-1186871245-1398891620-9085</Member>
    </AzApplicationGroup>
    <AzApplicationGroup Guid="94d83ca2-2d5a-47e0-a71a-424caabe1dbb" Name="CarRoddingShopSupervisor" Description="(production) " GroupType="Basic">
    <Member>S-1-5-21-1266324296-1186871245-1398891620-9086</Member>
    </AzApplicationGroup>
    <AzApplicationGroup Guid="94ad3311-4928-4a9b-9c9f-b4ae8a009a36" Name="CarGuest" Description="(production) " GroupType="Basic">
    <Member>S-1-5-21-1266324296-1186871245-1398891620-9088</Member>
    </AzApplicationGroup>
    <AzApplicationGroup Guid="b4e819d8-f9f1-4f23-b643-297827851a2e" Name="CarRoddingShopOperator" Description="(production) " GroupType="Basic">
    <Member>S-1-5-21-1266324296-1186871245-1398891620-9082</Member>
    </AzApplicationGroup>
    <AzApplicationGroup Guid="bb49e791-72ff-4359-a9e8-d9163c641590" Name="EnergyOperator" Description="(Production) User that can consult meters, meters balance information and also run reports" GroupType="Basic">
    <Member>S-1-5-21-1266324296-1186871245-1398891620-8940</Member>
    </AzApplicationGroup>

    Please consider this as an urgent request and help 

    Thanks in advance 

    Wednesday, October 9, 2019 9:38 AM

Answers

  • Hi pepcoder,

    If the "Authorization Manager file" is a "xml" file, you can use "System.Xml" to get specific node. The following is a simple demo.

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load("D:\\testxml.xml");
        string Temp = String.Empty;
        XmlNode nodes = xmldoc.SelectSingleNode("user");
        for (int i = 0; i < nodes.ChildNodes.Count; i++)
        {
            XmlNode c = nodes.ChildNodes[i];
            Temp = c.Attributes["name"].Value;
            Console.WriteLine(Temp);
        }

    The test xml file,

    <?xml version="1.0" encoding="UTF-8"?>
    <user>
      <persons ID="1" name="Tom" />
      <persons ID="2" name="Jerry" />
    </user>

    If it's not a "xml" file, you can also try regular expression.

    As to import the data into Excel file, you can refer the following code that import a datagridview (or a datatable) into an Excel file.

    public void ImporttoExcel(string saveFileName, DataGridView myDGV)
    {
        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
        if (xlApp == null)
        {
            MessageBox.Show("No Excel on this machine!");
            return;
        }
    
        Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
        Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
    
        // Write title
        for (int i = 0; i < myDGV.ColumnCount; i++)
        {
            worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
        }
        //Write data
        for (int r = 0; r < myDGV.Rows.Count; r++)
        {
            for (int i = 0; i < myDGV.ColumnCount; i++)
            {
                worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
            }
            System.Windows.Forms.Application.DoEvents();
        }
        worksheet.Columns.EntireColumn.AutoFit();
        if (saveFileName != "")
        {
            try
            {
                workbook.Saved = true;
                workbook.SaveCopyAs(saveFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("The file is occupied\n" + ex.Message);
                return;
            }
        }
        xlApp.Quit();
        GC.Collect();
    }
    
    private void btImport_Click(object sender, EventArgs e)
    {
        ImporttoExcel(@"D:/test.xls", dgvTestDeck);
    }

    Hope these can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 10, 2019 2:29 AM
    Moderator

All replies

  • Hi pepcoder,

    If the "Authorization Manager file" is a "xml" file, you can use "System.Xml" to get specific node. The following is a simple demo.

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load("D:\\testxml.xml");
        string Temp = String.Empty;
        XmlNode nodes = xmldoc.SelectSingleNode("user");
        for (int i = 0; i < nodes.ChildNodes.Count; i++)
        {
            XmlNode c = nodes.ChildNodes[i];
            Temp = c.Attributes["name"].Value;
            Console.WriteLine(Temp);
        }

    The test xml file,

    <?xml version="1.0" encoding="UTF-8"?>
    <user>
      <persons ID="1" name="Tom" />
      <persons ID="2" name="Jerry" />
    </user>

    If it's not a "xml" file, you can also try regular expression.

    As to import the data into Excel file, you can refer the following code that import a datagridview (or a datatable) into an Excel file.

    public void ImporttoExcel(string saveFileName, DataGridView myDGV)
    {
        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
        if (xlApp == null)
        {
            MessageBox.Show("No Excel on this machine!");
            return;
        }
    
        Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
        Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
    
        // Write title
        for (int i = 0; i < myDGV.ColumnCount; i++)
        {
            worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
        }
        //Write data
        for (int r = 0; r < myDGV.Rows.Count; r++)
        {
            for (int i = 0; i < myDGV.ColumnCount; i++)
            {
                worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
            }
            System.Windows.Forms.Application.DoEvents();
        }
        worksheet.Columns.EntireColumn.AutoFit();
        if (saveFileName != "")
        {
            try
            {
                workbook.Saved = true;
                workbook.SaveCopyAs(saveFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("The file is occupied\n" + ex.Message);
                return;
            }
        }
        xlApp.Quit();
        GC.Collect();
    }
    
    private void btImport_Click(object sender, EventArgs e)
    {
        ImporttoExcel(@"D:/test.xls", dgvTestDeck);
    }

    Hope these can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 10, 2019 2:29 AM
    Moderator
  • Thanks a lot Kyle Wang.  It works :-)
    Thursday, October 10, 2019 7:19 AM