Export (0) Print
Expand All

ReflectionPermission.ToXml Method

Creates an XML encoding of the permission and its current state.

[Visual Basic]
Overrides Public Function ToXml() As SecurityElement Implements _
   ISecurityEncodable.ToXml
[C#]
public override SecurityElement ToXml();
[C++]
public: SecurityElement* ToXml();
[JScript]
public override function ToXml() : SecurityElement;

Return Value

An XML encoding of the permission, including any state information.

Implements

ISecurityEncodable.ToXml

Example

[Visual Basic] 
' ToXml creates an XML encoding of the permission and its current state; 
' FromXml reconstructs a permission with the specified state from the XML encoding. 
Private Function ToFromXmlDemo() As Boolean

    Dim returnValue As Boolean = True

    Dim reflection1 As ReflectionPermissionFlag
    Dim reflectionPerm1, reflectionPerm2 As ReflectionPermission
    Dim reflectionGen1 As New ReflectionGenerator()
    Dim reflectionGen2 As New ReflectionGenerator()
    reflectionGen1.ResetIndex()
    While reflectionGen1.CreateReflection(reflectionPerm1, reflection1)
        If reflectionPerm1 Is Nothing Then
            GoTo ContinueWhile1
        End If
        Console.WriteLine("********************************************************" & ControlChars.Lf)
        reflectionGen2.ResetIndex()
        Try
            reflectionPerm2 = New ReflectionPermission(PermissionState.None)
            reflectionPerm2.FromXml(reflectionPerm1.ToXml())
            Console.WriteLine(("Result of ToFromXml = " & reflectionPerm2.ToString()))

        Catch e As Exception
            Console.WriteLine(("ToFromXml failed :" & reflectionPerm1.ToString() & e.ToString()))
            GoTo ContinueWhile1
        End Try
ContinueWhile1:
    End While

    Return returnValue
End Function 'ToFromXmlDemo


[C#] 
// ToXml creates an XML encoding of the permission and its current state; 
// FromXml reconstructs a permission with the specified state from the XML encoding. 
private bool ToFromXmlDemo()
{

    bool returnValue = true;

    ReflectionPermissionFlag reflection1;
    ReflectionPermission reflectionPerm1,reflectionPerm2;
    ReflectionGenerator reflectionGen1 = new ReflectionGenerator();
    ReflectionGenerator reflectionGen2 = new ReflectionGenerator();
    reflectionGen1.ResetIndex();
    while(reflectionGen1.CreateReflection(out reflectionPerm1, out reflection1)) 
    {
        if(reflectionPerm1 == null) continue;
        Console.WriteLine("********************************************************\n");
        reflectionGen2.ResetIndex();
        try
        {            
            reflectionPerm2= new ReflectionPermission(PermissionState.None);
            reflectionPerm2.FromXml(reflectionPerm1.ToXml());
            Console.WriteLine("Result of ToFromXml = " +
                reflectionPerm2.ToString());

        }
        catch(Exception e) 
        {
            Console.WriteLine("ToFromXml failed :" + reflectionPerm1.ToString() + e);
            continue;
        }
    }

    return returnValue;

}

[C++] 
// ToXml creates an XML encoding of the permission and its current state;
// FromXml reconstructs a permission with the specified state from the XML encoding.
private:
bool ToFromXmlDemo() {

   bool returnValue = true;

   ReflectionPermissionFlag reflection1;
   ReflectionPermission* reflectionPerm1, *reflectionPerm2;
   ReflectionGenerator* reflectionGen1 = new ReflectionGenerator();
   ReflectionGenerator* reflectionGen2 = new ReflectionGenerator();
   reflectionGen1->ResetIndex();
   while(reflectionGen1->CreateReflection(&reflectionPerm1, &reflection1)) {
      if (reflectionPerm1 == 0) continue;
      Console::WriteLine(S"********************************************************\n");
      reflectionGen2->ResetIndex();
      try {
         reflectionPerm2 = new ReflectionPermission(PermissionState::None);
         reflectionPerm2->FromXml(reflectionPerm1->ToXml());
         Console::WriteLine(S"Result of ToFromXml = {0}", reflectionPerm2);

      } catch (Exception* e) {
         Console::WriteLine(S"ToFromXml failed : {0}{1}", reflectionPerm1, e);
         continue;
      }
   }

   return returnValue;

}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

ReflectionPermission Class | ReflectionPermission Members | System.Security.Permissions Namespace

Show:
© 2015 Microsoft