Friday, April 20, 2012

How to call a method using Reflection in C#.net


public static object Reflection_Result(string Method, string DllName, string ClassName, object[] ParametersList)
        {
            object result;
            try
            {
                DllName = DllName.Replace(".dll", "");
                string dll = Path.GetFileName(DllName);

                Assembly assembly = Assembly.LoadFrom(DllName + ".dll");
                object obj1 = assembly.CreateInstance(dll + "." + ClassName);
                Type type1 = obj1.GetType();
                result = type1.GetMethod(Method).Invoke(obj1, ParametersList);
            }
            catch (Exception ex)
            {
                ErrorLogs.SaveError(ex, "COMMON_UTILITY");
                throw ex;
            }
            return result;
        }

Wednesday, April 11, 2012

Create XML File using C#.NET

public void Create_DB_XML(string TenantName,string ServerName,string DatabaseName,string UserName,string Password)

{

string filename = "C\\DataBaseDetails.xml";

if (File.Exists(filename))

File.Delete(filename);

XmlWriterSettings settings = new XmlWriterSettings();

settings.OmitXmlDeclaration = true;

settings.ConformanceLevel = ConformanceLevel.Fragment;

settings.CloseOutput = false;

StringBuilder sb = new StringBuilder();

XmlWriter writer = XmlWriter.Create(sb, settings);

writer.WriteStartElement("Databases", "");

writer.WriteStartElement("DB", "");

writer.WriteAttributeString("TenentId", TenantName);

writer.WriteElementString("userid", UserName);

writer.WriteElementString("password", Password);

writer.WriteElementString("database", DatabaseName);

writer.WriteElementString("server", ServerName);

writer.WriteEndElement();

writer.WriteEndElement();

writer.Flush();

writer.Close();

string ss = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";

ss = ss + sb.ToString();

StringReader stream = new StringReader(ss);

XmlTextReader reader = new XmlTextReader(stream);

XmlDocument dom = new XmlDocument();

dom.Load(reader);

dom.Save(filename);

}




|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
OutPut
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

<?
xml version="1.0" encoding="utf-8"?>

<Databases>

<DB TenentId="hosp">

<userid>uuuuuu</userid>

<password>pppppp</password>

<database>ddddddd</database>

<server>sssssssss</server>

</DB>

</Databases>