Friday, May 1, 2015

asp.net Merge XML

 public static string MergeXML(string xml1, string xml2)
        {
            XmlDocument xmlDoc1 = new XmlDocument();
            XmlDocument xmlDoc2 = new XmlDocument();
            xmlDoc1.LoadXml(xml1);
            xmlDoc2.LoadXml(xml2);
            XmlNode newNode;
            string mergedXML = string.Empty;
            for (int j = 0; j <= xmlDoc2.DocumentElement.ChildNodes.Count - 1; j++)
            {
                newNode = xmlDoc1.ImportNode(xmlDoc2.DocumentElement.ChildNodes[j], true);
                xmlDoc1.DocumentElement.AppendChild(newNode);
            }
            mergedXML = Functions.GetXmlString(xmlDoc1);

            return mergedXML;
        }

asp.net String To Bytes

  public static byte[] StringToBytes(string requestXML){
            byte[] data = null;
            try{
                Encoding enc = System.Text.Encoding.ASCII;
                data = enc.GetBytes(requestXML);              
            }
            catch (Exception ex){
                logger.LogInfo("Library.Functions.FileToBase64(): Exception: " + ex.ToString());
                return null;
            }
            return data;
        }

asp.net file to bytes

  public static byte[] FileToBytes(string filePath)
        {
            byte[] data = null;
            try
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                long numBytes = new FileInfo(filePath).Length;
                //logger.LogInfo("Library.Functions.FileToBase64(): Path:" + filePath + " Bytes:" + numBytes);
                data = br.ReadBytes((int)numBytes);
            }
            catch (Exception ex)
            {
                logger.LogInfo("Library.Functions.FileToBase64(): Exception: " + ex.ToString());
                return null;
            }
            return data;
        }

asp.net check is Child Class

  private static bool isChildClass(System.Reflection.PropertyInfo propertyInfo)
        {
            return (propertyInfo.PropertyType.IsClass && !propertyInfo.PropertyType.IsValueType &&
                                !propertyInfo.PropertyType.IsPrimitive && propertyInfo.PropertyType.FullName != "System.String");
        }

asp.net Get Temporary Folder Path

  public static string GetTempFolderPath()
        {
            return Path.GetTempPath();
        }

asp.net Delete Directory

   public static Boolean DeleteDirectory(string _targetFolder)                                                                  
        {
            try
            {
                if (Directory.Exists(_targetFolder))    
                {
                    Directory.Delete(_targetFolder, true);
                }
                return true;
            }
            catch (Exception ex)
            {
                logger.LogError("Library.Functions.DeleteDirectory(string):Target:" + _targetFolder + ": Exception: " + ex.ToString());
                return false;
            }
        }

asp.net Trim Zero

  public static string TrimZero(string byval)
        {
            string result = byval;
            int conertResult = 0;

            try
            {
                conertResult = Convert.ToInt32(byval);
                result = conertResult.ToString();
            }
            catch
            {
                return result;
            }

            return result;
        }