ToXml() public method

public ToXml ( ) : SecurityElement
return SecurityElement
Esempio n. 1
0
        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)
            {
            }

        }
        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
            {
                StringBuilder sb = new StringBuilder();
                Encoding encoding = Encoding.UTF8;

                SecurityElement format = new SecurityElement("xml");
                format.m_type = SecurityElementType.Format;
                format.AddAttribute("version", "1.0");
                format.AddAttribute("encoding", encoding.WebName);
                sb.Append(format.ToString());
                sb.Append(elConf.ToString());

                byte[] data = encoding.GetBytes(sb.ToString());

                // Write out the new config.
                if (level.Path == null || !Config.SaveDataByte(level.Path, data, 0, data.Length))
                    throw new PolicyException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_UnableToSave"), level.Label));
            }
            catch (Exception e)
            {
                if (e is PolicyException)
                    throw e;
                else
                    throw new PolicyException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_UnableToSave"), level.Label), e);
            }
            catch
            {
                throw new PolicyException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Policy_UnableToSave"), level.Label));
            }

            Config.ResetCacheData(level.ConfigId);
            if (CanUseQuickCache(level.RootCodeGroup))
                Config.SetQuickCache(level.ConfigId, GenerateQuickCache(level));
        }
        internal static void EncodeLevel (PolicyLevel level)
        {
            Contract.Assert(level != null, "No policy level to encode.");

            // We cannot encode a policy level without a backing file  
            if (level.Path == null)
            {
                string errorMessage = Environment.GetResourceString("Policy_UnableToSave",
                                                    level.Label,
                                                    Environment.GetResourceString("Policy_SaveNotFileBased"));
                throw new PolicyException(errorMessage);
            }
                
            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
            {
                StringBuilder sb = new StringBuilder();
                Encoding encoding = Encoding.UTF8;

                SecurityElement format = new SecurityElement("xml");
                format.m_type = SecurityElementType.Format;
                format.AddAttribute("version", "1.0");
                format.AddAttribute("encoding", encoding.WebName);
                sb.Append(format.ToString());
                sb.Append(elConf.ToString());

                byte[] data = encoding.GetBytes(sb.ToString());

                // Write out the new config.
                int hrSave = Config.SaveDataByte(level.Path, data, data.Length);
                Exception extendedError = Marshal.GetExceptionForHR(hrSave);
                if (extendedError != null)
                {
                    string extendedInformation = extendedError != null ? extendedError.Message : String.Empty;
                    throw new PolicyException(Environment.GetResourceString("Policy_UnableToSave", level.Label, extendedInformation), extendedError);
                }
            }
            catch (Exception e)
            {
                if (e is PolicyException)
                    throw e;
                else
                    throw new PolicyException(Environment.GetResourceString("Policy_UnableToSave", level.Label, e.Message), e);
            }

            Config.ResetCacheData(level.ConfigId);
            if (CanUseQuickCache(level.RootCodeGroup))
                Config.SetQuickCache(level.ConfigId, GenerateQuickCache(level));
        }
 internal static void EncodeLevel(PolicyLevel level)
 {
     if (level.Path == null)
     {
         throw new PolicyException(Environment.GetResourceString("Policy_UnableToSave", new object[] { level.Label, Environment.GetResourceString("Policy_SaveNotFileBased") }));
     }
     SecurityElement element = new SecurityElement("configuration");
     SecurityElement child = new SecurityElement("mscorlib");
     SecurityElement element3 = new SecurityElement("security");
     SecurityElement element4 = new SecurityElement("policy");
     element.AddChild(child);
     child.AddChild(element3);
     element3.AddChild(element4);
     element4.AddChild(level.ToXml());
     try
     {
         StringBuilder builder = new StringBuilder();
         Encoding encoding = Encoding.UTF8;
         SecurityElement element5 = new SecurityElement("xml") {
             m_type = SecurityElementType.Format
         };
         element5.AddAttribute("version", "1.0");
         element5.AddAttribute("encoding", encoding.WebName);
         builder.Append(element5.ToString());
         builder.Append(element.ToString());
         byte[] bytes = encoding.GetBytes(builder.ToString());
         Exception exceptionForHR = Marshal.GetExceptionForHR(Config.SaveDataByte(level.Path, bytes, bytes.Length));
         if (exceptionForHR != null)
         {
             string str2 = (exceptionForHR != null) ? exceptionForHR.Message : string.Empty;
             throw new PolicyException(Environment.GetResourceString("Policy_UnableToSave", new object[] { level.Label, str2 }), exceptionForHR);
         }
     }
     catch (Exception exception2)
     {
         if (exception2 is PolicyException)
         {
             throw exception2;
         }
         throw new PolicyException(Environment.GetResourceString("Policy_UnableToSave", new object[] { level.Label, exception2.Message }), exception2);
     }
     Config.ResetCacheData(level.ConfigId);
     if (CanUseQuickCache(level.RootCodeGroup))
     {
         Config.SetQuickCache(level.ConfigId, GenerateQuickCache(level));
     }
 }