This documentation is archived and is not being maintained.

DnsPermission.FromXml Method

Reconstructs a DnsPermission instance from an XML encoding.

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

Parameters

securityElement
The XML encoding to use to reconstruct the DnsPermission instance.

Implements

ISecurityEncodable.FromXml

Exceptions

Exception Type Condition
ArgumentNullException securityElement is a null reference (Nothing in Visual Basic).
ArgumentException securityElement is not a DnsPermission element.

Remarks

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

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

Example

[Visual Basic, C#, C++] The following example reconstructs a DnsPermission instance from an XML encoding.

[Visual Basic] 
Public Sub ConstructDnsPermission()
    Try
        ' Create a DnsPermission instance.
        Dim permission As New DnsPermission(PermissionState.None)
        ' Create a SecurityElement instance by calling the ToXml method on the
        ' DnsPermission instance and 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 instacnce .
        Dim securityElementObj As New SecurityElement("IPermission")
        ' Add attributes and values of the SecurityElement instance corresponding to
        ' teh permission instance.
        securityElementObj.AddAttribute("version", "1")
        securityElementObj.AddAttribute("Unrestricted", "true")
        securityElementObj.AddAttribute("class", "System.Net.DnsPermission")
        
        ' Reconstruct a DnsPermission instance from an XML encoding.
        Dim permission1 As 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().Attributes)
    Catch e As NullReferenceException
        Console.WriteLine("NullReferenceException caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    Catch e As SecurityException
        Console.WriteLine("SecurityException caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    Catch e As ArgumentNullException
        Console.WriteLine("ArgumentNullException caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    Catch e As Exception
        Console.WriteLine("Exception caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    End Try
End Sub 'ConstructDnsPermission


Private Sub PrintKeysAndValues(myList As Hashtable)
    ' Get the enumerator that can iterate through the hash table.
    Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
    Console.WriteLine(ControlChars.Tab + "-KEY-" + ControlChars.Tab + "-VALUE-")
    While myEnumerator.MoveNext()
        Console.WriteLine(ControlChars.Tab + "{0}:" + ControlChars.Tab + "{1}", myEnumerator.Key, myEnumerator.Value)
    End While
    Console.WriteLine()
End Sub 'PrintKeysAndValues


[C#] 
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().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().Attributes);
  }
  catch(NullReferenceException e) 
  {
    Console.WriteLine("NullReferenceException caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
  }
  catch(SecurityException e) 
  {
    Console.WriteLine("SecurityException caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
  }
  catch(ArgumentNullException e) 
  {
    Console.WriteLine("ArgumentNullException caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
  }
  catch(Exception e)
  {
    Console.WriteLine("Exception caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + 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();
}

[C++] 
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(S"Attributes and Values of 'DnsPermission' instance :");
         PrintKeysAndValues(permission->ToXml()->Attributes);

         // Create a SecurityElement instance.
         SecurityElement* securityElementObj = new SecurityElement(S"IPermission");
         // Add attributes and values of the SecurityElement instance corresponding to
         // the permission instance.
         securityElementObj->AddAttribute(S"version", S"1");
         securityElementObj->AddAttribute(S"Unrestricted", S"true");
         securityElementObj->AddAttribute(S"class", S"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(S"After reconstruction Attributes and Values of new DnsPermission instance :");
         PrintKeysAndValues(permission1->ToXml()->Attributes);
      }
      catch (NullReferenceException* e)
      {
         Console::WriteLine(S"NullReferenceException caught!!!");
         Console::WriteLine(S"Source : {0}", e->Source);
         Console::WriteLine(S"Message : {0}", e->Message);
      }
      catch (SecurityException* e)
      {
         Console::WriteLine(S"SecurityException caught!!!");
         Console::WriteLine(S"Source : {0}", e->Source);
         Console::WriteLine(S"Message : {0}", e->Message);
      }
      catch (ArgumentNullException* e)
      {
         Console::WriteLine(S"ArgumentNullException caught!!!");
         Console::WriteLine(S"Source : {0}", e->Source);
         Console::WriteLine(S"Message : {0}", e->Message);
      }
      catch (Exception* e)
      {
         Console::WriteLine(S"Exception caught!!!");
         Console::WriteLine(S"Source : {0}", e->Source);
         Console::WriteLine(S"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(S"\t-KEY-\t-VALUE-");
      while (myEnumerator->MoveNext())
         Console::WriteLine(S"\t {0}:\t {1}", 
         myEnumerator->Key, myEnumerator->Value);
      Console::WriteLine();
   }

[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

DnsPermission Class | DnsPermission Members | System.Net Namespace | SecurityElement

Show: