Wednesday, September 28, 2011

Remove Different part of string within string by using Two Dimensional Array


This is use to remove the string just like From index To  index functionality.


You just need to send the Original text with list of string array two dimensional where Ist dimension contain the row number and second one contain the column number.i.e. original string is ="abcdefghijklmnop"  listToRemove[0,0]="cd" ,  listToRemove[0,1]="ghi"
it means that you want to remove from "cd" to "ghi" (i.e.  cdefghi)
so your result will be "abjklmnop"

/// <summary>
        /// wajid shah
        /// </summary>
        /// <param name="OriginalStr">Original String</param>
        /// <param name="listToRemove">Two dimensional String list to be remove from the original string </param>
        /// <param name="Counter">Number of item in list. This is use for loop purpose.  i.e.  string[9,2] then you should send counter value to 9</param>
        /// <returns></returns>
        public static string RemoveSubStringFromString(string OriginalStr, string[,] listToRemove, int Counter)
        {
            string FinalStr = string.Empty;
            bool Flag = false;
            string[] FirstStr;
            string[] LastStr;

            for (int i = 0; i <= Counter-1; i++)
            {

                if (OriginalStr.Contains(listToRemove[i, 0].ToString()))
                {
                    if (!Flag)
                    {
                        Flag = true;
                       
                        FirstStr = OriginalStr.Split(new string[] { listToRemove[i, 0] }, StringSplitOptions.RemoveEmptyEntries);

                        LastStr = FirstStr[1].ToString().Split(new string[] { listToRemove[i, 1] }, StringSplitOptions.RemoveEmptyEntries);

                        FinalStr = FirstStr[0].ToString() + LastStr[1].ToString();
                    }
                    else
                    {
                        FirstStr = FinalStr.Split(new string[] { listToRemove[i, 0] }, StringSplitOptions.RemoveEmptyEntries);

                        LastStr = FirstStr[1].ToString().Split(new string[] { listToRemove[i, 1] }, StringSplitOptions.RemoveEmptyEntries);

                        FinalStr = FirstStr[0].ToString() + LastStr[1].ToString();
                    }
                }
            }
            return FinalStr;
        }




use like
OriginalString ="<SaveSearch><MajorMarketAreaID>147</MajorMarketAreaID><PropertyDirection>N</PropertyDirection><City>southfield</City><State>michigan</State><Latitude>42.4733688</Latitude><Longitude>-83.2218731</Longitude><Radius>3</Radius><PropertyType>1</PropertyType><TransactionSpaceType>0</TransactionSpaceType></SaveSearch>"


 string[,] str=new string[8,2];

              str[0, 0] = "<TenantStreetNumberFrom>";
              str[0, 1] = "</TenantStreetNumberFrom>";

              str[1, 0] = "<TenantStreetNumberTo>";
              str[1, 1] = "</TenantStreetNumberTo>";
           
              str[2, 0] = "<PropertyDirection>";
              str[2, 1] = "</PropertyDirection>";

              str[3, 0] = "<TenantStreetName>";
              str[3, 1] = "</TenantStreetName>";

              str[4, 0] = "<City>";
              str[4, 1] = "</City>";

              str[5, 0] = "<County>";
              str[5, 1] = "</County>";

              str[6, 0] = "<State>";
              str[6, 1] = "</State>";

              str[7, 0] = "<Zip>";
              str[7, 1] = "</Zip>";

              //str[8, 0] = "<Radius>";
              //str[8, 1] = "</Radius>";
             
             
             string Query1=RemoveSubStringFromString(OriginalString, str,8);


Result String ="<SaveSearch><MajorMarketAreaID>147</MajorMarketAreaID><Latitude>42.4733688</Latitude><Longitude>-83.2218731</Longitude><PropertyType>1</PropertyType><TransactionSpaceType>0</TransactionSpaceType></SaveSearch>"


Friday, September 23, 2011

XML File to XML Schema or XML to XSD

if you have xml file and want to create its xml schema or XSD then you just upload your file to this website and resultant you can see the xsd or xml schema.

http://www.flame-ware.com/products/xml-2-xsd/

same in the case for xsd to xml

Tuesday, September 6, 2011

Get User IP address in asp.net


We have Request.UserHostAddress to get the IP address in ASP.NET, but this is usually the user's ISP's IP address, not exactly the user's machine IP address who for example clicked a link


HttpContext.Current.Request.UserHostAddress; 
or
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
To get the IP address of the machine and not the proxy use the following code
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];



 private string getUserIPAddress()
    {
        string strHostName = System.Net.Dns.GetHostName();
        string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
        return clientIPAddress;


    }