A day with .Net

My day to day experince in .net

Archive for March, 2011

Create vertical text in internet explorer and mozilla using CSS

Posted by vivekcek on March 11, 2011

Do u want to show vertical text in browsers like below.

This is the code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        .test
        {
            display: block;
            writing-mode: tb-rl;
            -webkit-transform: rotate(-90deg);
            -moz-transform: rotate(-90deg);
            position: absolute;
            float: left;
        }
    </style>
</head>
<body>
    <table>
        <tr>
            <td class="test" style="font-size: xx-large">
                vivek
            </td>
        </tr>
    </table>
</body>
</html>
Advertisements

Posted in Uncategorized | Leave a Comment »

Adding assembly specific application configuration file

Posted by vivekcek on March 10, 2011

Hi friends vivek here.So you want to add a confifuration file to one of your assembly,and that assembly is used in an ASP.NET application.You cant achieve it directly. Because one aplication domain of ASP.NET support only one configuration file thats the web.config.But dont worry i have a solution for that.

1.Create an aseembly named Logger.
2.Add an application configuration file to this assembly name it as Logger.dll.config(The config file name should conatin your assembly name).
3.Wrte this tag in your configuraion file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <assemblySettings>
    <add key="OTA" value="Data Source=server\SQL2008;Initial Catalog=db;User ID=sdfx;Password=sdfs;Provider=SQLOLEDB"/>
  </assemblySettings>
</configuration>

4.Add a class named AssemblySettings.cs in your library with the below code.

using System;
using System.Reflection;
using System.Collections;
using System.Xml;
using System.Configuration;
using System.Runtime.CompilerServices;


namespace OTA.API.Logger
{
    internal class AssemblySettings
    {
        private IDictionary settings;

        [MethodImpl(MethodImplOptions.NoInlining)]
        public AssemblySettings()
            : this(Assembly.GetCallingAssembly())
        {
        }

        public AssemblySettings(Assembly asm)
        {
            settings = GetConfig(asm);
        }

        public string this[string key]
        {
            get
            {
                string settingValue = null;

                if (settings != null)
                {
                    settingValue = settings[key] as string;
                }

                return (settingValue == null ? "" : settingValue);
            }
        }

        public static IDictionary GetConfig()
        {
            return GetConfig(Assembly.GetCallingAssembly());
        }

        public static IDictionary GetConfig(Assembly asm)
        {
            // Open and parse configuration file for specified
            // assembly, returning collection to caller for future
            // use outside of this class.
            //
            try
            {
                string cfgFile = asm.CodeBase + ".config";
                const string nodeName = "assemblySettings";

                XmlDocument doc = new XmlDocument();
                doc.Load(new XmlTextReader(cfgFile));

                XmlNodeList nodes = doc.GetElementsByTagName(nodeName);

                foreach (XmlNode node in nodes)
                {
                    if (node.LocalName == nodeName)
                    {
                        DictionarySectionHandler handler = new DictionarySectionHandler();
                        return (IDictionary)handler.Create(null, null, node);
                    }
                }
            }
            catch
            {
            }

            return (null);
        }
    }
}

5.Create a class Log.cs from that class you can acess config file value as

AssemblySettings ASM = new AssemblySettings(System.Reflection.Assembly.GetExecutingAssembly());
String ConnectionString = ASM["OTA"].ToString(); 

Posted in Uncategorized | Leave a Comment »

Validate an XML aganist an XSD Schema stored as embeded resource in a DLL

Posted by vivekcek on March 10, 2011

STEPS
—–

1.Add the schema to your library
2.Right click on the schmema file select properties set build action as embed resuorce.
3.Write a class XMLSchema in your library as shown below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
using System.Data;
namespace XMLParser
{
    public class XMLSchema
    {
        public bool ValidateXml(string xml,string xsdName)
        {
            try
            {
                //Declare a StreamReader and a StreamWriter
                StreamReader strmrStreamReader;
                //Extract the Resource using this line. Notice the namespace... ReadWriteResourceFromDLLorEXE!
                strmrStreamReader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("XMLParser."+xsdName));
                //Here we are creating a new file in the Application Startup path, and for fun
                //we are also changing the name to NewFile.txt
                System.Xml.Schema.XmlSchema xSchema = new System.Xml.Schema.XmlSchema();
                xSchema = XmlSchema.Read(strmrStreamReader, null);
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.Schemas.Add(xSchema);
                settings.ValidationType = ValidationType.Schema;
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xml);
                XmlReader rdr = XmlReader.Create(new StringReader(xDoc.InnerXml), settings);
                while (rdr.Read())
                { }
               
            }
            catch
            {
                return false;
            }
            return true;


        }
        public bool ValidateXml(XmlDocument xml)
        {
            return false;
        }
    }
}

4.Create an instance of the XmlSchema class and call the method ValidateXml() with your xml and,the name of XSD u add as resource in your dll

Posted in c#.net | Leave a Comment »