internal static void EncodeLevel( PolicyLevel level )
{
SecurityElement elConf = new SecurityElement( "configuration" );
SecurityElement elMscorlib = new SecurityElement( "mscorlib" );
SecurityElement elSecurity = new SecurityElement( "security" );
SecurityElement elPolicy = new SecurityElement( "policy" );
elConf.AddChild( elMscorlib );
elMscorlib.AddChild( elSecurity );
elSecurity.AddChild( elPolicy );
elPolicy.AddChild( level.ToXml() );
try
{
MemoryStream stream = new MemoryStream( 24576 );
StreamWriter writer = new StreamWriter( stream, new UTF8Encoding(false) );
Encoding encoding = level.Encoding;
if (encoding == null)
encoding = writer.Encoding;
SecurityElement format = new SecurityElement( "xml" );
format.m_type = SecurityElementType.Format;
format.AddAttribute( "version", "1.0" );
format.AddAttribute( "encoding", encoding.WebName );
writer.Write( format.ToString() );
writer.Flush();
writer = new StreamWriter( stream, encoding );
writer.Write( elConf.ToString() );
writer.Flush();
// Write out the new config.
if (!Config.SaveData( level.ConfigId, stream.GetBuffer(), 0, (int)stream.Length ))
{
throw new PolicyException( String.Format( Environment.GetResourceString( "Policy_UnableToSave" ), level.Label ) );
}
}
catch (Exception e)
{
if (e is PolicyException)
throw e;
else
throw new PolicyException( String.Format( Environment.GetResourceString( "Policy_UnableToSave" ), level.Label ), e );
}
Config.ResetCacheData( level.ConfigId );
try
{
if (CanUseQuickCache( level.RootCodeGroup ))
{
Config.SetQuickCache( level.ConfigId, GenerateQuickCache( level ) );
}
}
catch (Exception)
{
}
}