Export (0) Print
Expand All

DnsPermission.FromXml Method

Reconstructs a DnsPermission instance from an XML encoding.

Namespace: System.Net
Assembly: System (in system.dll)

public:
virtual void FromXml (
	SecurityElement^ securityElement
) override
public void FromXml (
	SecurityElement securityElement
)
public override function FromXml (
	securityElement : SecurityElement
)
Not applicable.

Parameters

securityElement

The XML encoding to use to reconstruct the DnsPermission instance.

Exception typeCondition

ArgumentNullException

securityElement is a null reference (Nothing in Visual Basic).

ArgumentException

securityElement is not a DnsPermission element.

The FromXml method reconstructs a DnsPermission instance from an XML encoding defined by the SecurityElement class.

Use the ToXml method to XML-encode the DnsPermission instance, including state information.

The following example reconstructs a DnsPermission instance from an XML encoding.

public:
   void ConstructDnsPermission()
   {
      try
      {
         // Create a DnsPermission instance.
         DnsPermission^ permission = gcnew DnsPermission( PermissionState::None );
         // Create a SecurityElement instance by calling the ToXml method on the
         // DnsPermission instance.
         // Print its attributes, which hold the  XML encoding of the DnsPermission
         // instance.
         Console::WriteLine( "Attributes and Values of 'DnsPermission' instance :" );
         PrintKeysAndValues( permission->ToXml()->Attributes );
         
         // Create a SecurityElement instance.
         SecurityElement^ securityElementObj = gcnew SecurityElement( "IPermission" );
         // Add attributes and values of the SecurityElement instance corresponding to
         // the permission instance.
         securityElementObj->AddAttribute( "version", "1" );
         securityElementObj->AddAttribute( "Unrestricted", "true" );
         securityElementObj->AddAttribute( "class", "System.Net.DnsPermission" );
         
         // Reconstruct a DnsPermission instance from an XML encoding.
         DnsPermission^ permission1 = gcnew DnsPermission( PermissionState::None );
         permission1->FromXml( securityElementObj );
         
         // Print the attributes and values of the constructed DnsPermission Object*.
         Console::WriteLine( "After reconstruction Attributes and Values of new DnsPermission instance :" );
         PrintKeysAndValues( permission1->ToXml()->Attributes );
      }
      catch ( NullReferenceException^ e ) 
      {
         Console::WriteLine( "NullReferenceException caught!!!" );
         Console::WriteLine( "Source : {0}", e->Source );
         Console::WriteLine( "Message : {0}", e->Message );
      }
      catch ( SecurityException^ e ) 
      {
         Console::WriteLine( "SecurityException caught!!!" );
         Console::WriteLine( "Source : {0}", e->Source );
         Console::WriteLine( "Message : {0}", e->Message );
      }
      catch ( ArgumentNullException^ e ) 
      {
         Console::WriteLine( "ArgumentNullException caught!!!" );
         Console::WriteLine( "Source : {0}", e->Source );
         Console::WriteLine( "Message : {0}", e->Message );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Exception caught!!!" );
         Console::WriteLine( "Source : {0}", e->Source );
         Console::WriteLine( "Message : {0}", e->Message );
      }
   }

private:
   void PrintKeysAndValues( Hashtable^ myList )
   {
      // Get the enumerator that can iterate through the hash table.
      IDictionaryEnumerator^ myEnumerator = myList->GetEnumerator();
      Console::WriteLine( "\t-KEY-\t-VALUE-" );
      while ( myEnumerator->MoveNext() )
      {
         Console::WriteLine( "\t {0}:\t {1}", myEnumerator->Key, myEnumerator->Value );
      }
      Console::WriteLine();
   }

public void ConstructDnsPermission()
{
    try {
        // Create a DnsPermission instance.
        DnsPermission permission = 
            new DnsPermission(PermissionState.None);

        // Create a SecurityElement instance by calling the ToXml 
        // method on the DnsPermission instance.
        // Print its attributes, which hold the  XML encoding of the 
        // DnsPermission instance.
        Console.WriteLine("Attributes and Values of 'DnsPermission' "
            + "instance :");
        PrintKeysAndValues(permission.ToXml().get_Attributes());

        // Create a SecurityElement instance.
        SecurityElement securityElementObj = 
            new SecurityElement("IPermission");

        // Add attributes and values of the SecurityElement instance 
        // corresponding to the permission instance. 
        securityElementObj.AddAttribute("version", "1");
        securityElementObj.AddAttribute("Unrestricted", "true");
        securityElementObj.AddAttribute("class", 
            "System.Net.DnsPermission");

        // Reconstruct a DnsPermission instance from an XML encoding.
        DnsPermission permission1 = 
            new DnsPermission(PermissionState.None);
        permission1.FromXml(securityElementObj);

        // Print the attributes and values of the constructed
        // DnsPermission object.
        Console.WriteLine("After reconstruction Attributes and Values "
            + "of new DnsPermission instance :");
        PrintKeysAndValues(permission1.ToXml().get_Attributes());
    }
    catch (NullReferenceException e) {
        Console.WriteLine("NullReferenceException caught!!!");
        Console.WriteLine("Source : " + e.get_Source());
        Console.WriteLine("Message : " + e.get_Message());
    }
    catch (SecurityException e) {
        Console.WriteLine("SecurityException caught!!!");
        Console.WriteLine("Source : " + e.get_Source());
        Console.WriteLine("Message : " + e.get_Message());
    }
    catch (ArgumentNullException e) {
        Console.WriteLine("ArgumentNullException caught!!!");
        Console.WriteLine("Source : " + e.get_Source());
        Console.WriteLine("Message : " + e.get_Message());
    }
    catch (System.Exception e) {
        Console.WriteLine("Exception caught!!!");
        Console.WriteLine("Source : " + e.get_Source());
        Console.WriteLine("Message : " + e.get_Message());
    }
} //ConstructDnsPermission

private void PrintKeysAndValues(Hashtable myList)
{
    // Get the enumerator that can iterate through the hash table.
    IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
    Console.WriteLine("\t-KEY-\t-VALUE-");
    while (myEnumerator.MoveNext()) {
        Console.WriteLine("\t{0}:\t{1}", myEnumerator.get_Key(),
            myEnumerator.get_Value());
    }
    Console.WriteLine();
} //PrintKeysAndValues

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft