Export (0) Print
Expand All

SecurityPermission.FromXml Method

Reconstructs a permission with a specified state from an XML encoding.

[Visual Basic]
Overrides Public Sub FromXml( _
   ByVal esd As SecurityElement _
) Implements ISecurityEncodable.FromXml
[C#]
public override void FromXml(
 SecurityElement esd
);
[C++]
public: void FromXml(
 SecurityElement* esd
);
[JScript]
public override function FromXml(
   esd : SecurityElement
);

Parameters

esd
The XML encoding to use to reconstruct the permission.

Implements

ISecurityEncodable.FromXml

Exceptions

Exception Type Condition
ArgumentNullException The esd parameter is a null reference (Nothing in Visual Basic).
ArgumentException The esd parameter is not a valid permission element.

-or-

The esd parameter's version number is not supported.

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 Security1 As SecurityPermissionFlag
    Dim SecurityPerm1, SecurityPerm2 As SecurityPermission
    Dim SecurityGen1 As New SecurityGenerator()
    Dim SecurityGen2 As New SecurityGenerator()
    SecurityGen1.ResetIndex()
    While SecurityGen1.CreateSecurity(SecurityPerm1, Security1)
        If SecurityPerm1 Is Nothing Then
            GoTo ContinueWhile1
        End If
        Console.WriteLine("********************************************************" & ControlChars.Lf)
        SecurityGen2.ResetIndex()
        Try
            SecurityPerm2 = New SecurityPermission(PermissionState.None)
            SecurityPerm2.FromXml(SecurityPerm1.ToXml())
            Console.WriteLine(("Result of ToFromXml = " & SecurityPerm2.ToString()))

        Catch e As Exception
            Console.WriteLine(("ToFromXml failed :" & SecurityPerm1.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;

    SecurityPermissionFlag Security1;
    SecurityPermission SecurityPerm1,SecurityPerm2;
    SecurityGenerator SecurityGen1 = new SecurityGenerator();
    SecurityGenerator SecurityGen2 = new SecurityGenerator();
    SecurityGen1.ResetIndex();
    while(SecurityGen1.CreateSecurity(out SecurityPerm1, out Security1)) 
    {
        if(SecurityPerm1 == null) continue;
        Console.WriteLine("********************************************************\n");
        SecurityGen2.ResetIndex();
        try
        {            
            SecurityPerm2= new SecurityPermission(PermissionState.None);
            SecurityPerm2.FromXml(SecurityPerm1.ToXml());
            Console.WriteLine("Result of ToFromXml = " +
                SecurityPerm2.ToString());

        }
        catch(Exception e) 
        {
            Console.WriteLine("ToFromXml failed :" + SecurityPerm1.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. 
bool ToFromXmlDemo()
{

    bool returnValue = true;

    SecurityPermissionFlag Security1;
    SecurityPermission* SecurityPerm1;
    SecurityPermission* SecurityPerm2;
    SecurityGenerator* SecurityGen1 = new SecurityGenerator();
    SecurityGenerator* SecurityGen2 = new SecurityGenerator();
    SecurityGen1->ResetIndex();
    while(SecurityGen1->CreateSecurity(&SecurityPerm1, &Security1)) 
    {
        if(SecurityPerm1 == 0) continue;
        Console::WriteLine(S"********************************************************\n");
        SecurityGen2->ResetIndex();
        try
        {            
            SecurityPerm2= new SecurityPermission(PermissionState::None);
            SecurityPerm2->FromXml(SecurityPerm1->ToXml());
            Console::WriteLine(S"Result of ToFromXml = {0}", SecurityPerm2);

        }
        catch(Exception* e) 
        {
            Console::WriteLine(S"ToFromXml failed :{0}{1}", SecurityPerm1, 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

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

Show:
© 2014 Microsoft