Share via


How to: View and Extract XML Data

This topic discusses how to use XSLT to view information in the XML that Forefront Identity Manager Synchronization Service (FIM Synchronization Service) can supply. It also explains how to use the Microsoft .NET Framework classes to extract information from the XML that FIM Synchronization Service can supply.

Using XSLT to View XML Data

The following example shows how to view some of the elements in a CsExport XML file using XSLT. It displays the information for the <attr> of <delta> elements with a displayName, employeeStatus, or employeeType attribute for any object in a <synchronized-hologram> section.

To make this XSLT work, you must insert the following lines into the XML file as the first lines, where CsExport.xsl is the name of the file containing the XSL commands:

<?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="CsExport.xsl"?>

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>

<xsl:template match="/">

<TABLE border = "2" bgcolor="#FFFFFF">
  <TR>
    <TH>Display Name</TH>
    <TH>Employee Status</TH>
    <TH>Employee Type</TH>
  </TR>

  <xsl:for-each select="/cs-objects/cs-object">
  <xsl:variable name="object" select="."/>

    <TR>
      <xsl:for-each select="$object/synchronized-hologram/entry/attr">
        <xsl:variable name="attribute" select="."/>

        <xsl:if test="$attribute/@name = 'displayName'">
          <TD>
            <xsl:value-of select="$attribute/value"/>
          </TD>
        </xsl:if>

        <xsl:if test="$attribute/@name = 'employeeStatus'">
          <TD>
            <xsl:value-of select="$attribute/value"/>
          </TD>
        </xsl:if>

        <xsl:if test="$attribute/@name = 'employeeType'">
          <TD>
            <xsl:value-of select="$attribute/value"/>
          </TD>
        </xsl:if>

      </xsl:for-each>
    </TR>

    </xsl:for-each>
</TABLE>

</xsl:template>
</xsl:stylesheet>

Extracting Information from XML Data

The following code example shows how to extract some of the elements in a CsExport XML file. It displays the information for the <attr> of <delta> elements with a displayName, employeeStatus, or employeeType attribute for any object in a <synchronized-hologram> section.

Imports System
Imports System.IO
Imports System.Text
Imports System.Xml

Module Module1

    Sub Main()

        ' The hologram
        Dim sHologram      As String = "cs-object/synchronized-hologram/"

        Dim DisplayName    As String = sHologram + "entry/attr[@name='displayName']/value"
        Dim EmployeeStatus As String = sHologram + "entry/attr[@name='employeeStatus']/value"
        Dim EmployeeType   As String = sHologram + "entry/attr[@name='employeeType']/value"

        Console.WriteLine("Enter the name of the XML file")
        filename = Console.ReadLine()

        Try
            Dim mySR   As StreamReader  = File.OpenText(filename)
            Dim reader As XmlTextReader = new XmlTextReader(mySR)
            Dim doc    As XmlDocument   = new XmlDocument()
            
            doc.Load(reader);

            dim attrNode As XmlNode

            attrNode = doc.DocumentElement.SelectSingleNode(DisplayName)

            If Not attrNode Is Nothing Then
                Console.WriteLine("DisplayName: " + attrNode.InnerText)
            End If

            attrNode = doc.DocumentElement.SelectSingleNode(EmployeeStatus)

            If Not attrNode Is Nothing Then
                Console.WriteLine("EmployeeStatus: " + attrNode.InnerText)
            End If

            attrNode = doc.DocumentElement.SelectSingleNode(EmployeeType)

            If Not attrNode Is Nothing Then
                Console.WriteLine("EmployeeType: " + attrNode.InnerText)
            End If
        Catch xe As XmlException
            Console.WriteLine("XML Exception: " + xe.Message)
        Catch fnfe As FileNotFoundException
            Console.WriteLine("Exception: " + fnfe.Message)
        End Try
    End Sub
End Module
using System;
using System.IO;
using System.Text;
using System.Xml;

public class getAttr
{
   [STAThread]
   static void Main(string[] args)
   {

      // The hologram
      string sHologram       = "cs-object/synchronized-hologram/";

      string DisplayName    = sHologram + "entry/attr[@name='displayName']/value";
      string EmployeeStatus = sHologram + "entry/attr[@name='employeeStatus']/value";
      string EmployeeType   = sHologram + "entry/attr[@name='employeeType']/value"; 

      Console.WriteLine("Enter the name of the XML file");
      String filename = Console.ReadLine();

      try
      {
         StreamReader  mySR   = File.OpenText(filename);
         XmlTextReader reader = new XmlTextReader(mySR);
         XmlDocument doc      = new XmlDocument();
            
         doc.Load(reader);

         XmlNode attrNode;

         attrNode = doc.DocumentElement.SelectSingleNode(DisplayName);

         if(!(attrNode == null))
         {
            Console.WriteLine("DisplayName: " + attrNode.InnerText);
         }

         attrNode = doc.DocumentElement.SelectSingleNode(EmployeeStatus);

         if(!(attrNode == null))
         {
            Console.WriteLine("EmployeeStatus: " + attrNode.InnerText);
         }

         attrNode = doc.DocumentElement.SelectSingleNode(EmployeeType);

         if(!(attrNode == null))
         {
            Console.WriteLine("EmployeeType: " + attrNode.InnerText);
         }
      }
      catch(XmlException xe)
      {
         Console.WriteLine("XML Exception: " + xe.Message);
      }
      catch(FileNotFoundException fnfe)
      {
         Console.WriteLine("Exception: " + fnfe.Message);
      }
   }
}  // To compile: csc /r:System.Xml.dll /out:getAttr.exe getAttr.cs

See Also

Concepts

XML Reference