Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#

Customize an application with XML fragments

Rate me:
Please Sign up or sign in to vote.
1.44/5 (3 votes)
22 May 20073 min read 28.2K   124   10  
How to customize an application using XML fragments
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using Bulasoft.Common.Properties;

namespace Bulasoft.Common.Serialization
{
    public class XmlHelpers
    {
        public const string xmlElementNamespace = "elements";
        public const string xmlElementNamespacePrefix = "e";

        public const string xmlVersioningNamespace = "versioning";
        public const string xmlVersioningNamespacePrefix = "o";

        public const string xmlAttributeNamespace = "";

        public static readonly char[] cdataChars = new char[] { '\r', '\n', '>', '<', '&' };

        public static string BoolToString(bool? value)
        {
            switch (value)
            {
                case true: return "1";
                case false: return "0";
                default: return "";
            }
        }

        public static bool? StringToBool(string value)
        {
            switch (value)
            {
                case "0": return false;
                case "1": return true;
                default: return null;
            }
        }

        public static string DateTimeToString(DateTime? value) {
            if ( value.HasValue)
                return XmlConvert.ToString(value.Value, XmlDateTimeSerializationMode.RoundtripKind);
            else
                return "";
        }

        private static TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
        private const string CrypParam = "MD5CryptoServiceProvider";
        private static byte[] key = MD5CryptoServiceProvider.Create().ComputeHash(Encoding.UTF8.GetBytes(CrypParam));

        public static string Encrypt(string value)
        {
            if (string.IsNullOrEmpty(value))
                return null;
            byte[] bytes = Encoding.UTF8.GetBytes(value);
            using (MemoryStream mStream = new MemoryStream())
            {
                using (CryptoStream cStream = new CryptoStream(mStream, tripleDES.CreateEncryptor(key, Encoding.ASCII.GetBytes(CrypParam)), CryptoStreamMode.Write))
                {
                    cStream.Write(bytes, 0, bytes.Length);
                    cStream.FlushFinalBlock();
                    return Convert.ToBase64String(mStream.ToArray());
                }
            }
        }

        public static string Decrypt(string value)
        {
            if (string.IsNullOrEmpty(value))
                return null;

            byte[] bytes = Convert.FromBase64String(value);
            byte[] result = new byte[bytes.Length];

            using (MemoryStream mStream = new MemoryStream(bytes))
            {
                using (CryptoStream cStream = new CryptoStream(mStream, tripleDES.CreateDecryptor(key, Encoding.ASCII.GetBytes(CrypParam)), CryptoStreamMode.Read))
                {
                    cStream.Read(result, 0, result.Length);                    
                    return Encoding.UTF8.GetString(result);
                }
            }
        }
        public static XmlElement GetChildNode(XmlElement element, string childName, string childURI)
        {
            foreach (XmlElement e in element.ChildNodes)
            {
                if ((e.LocalName == childName) && (e.NamespaceURI == childURI))
                    return e;
            }
            return null;
        }

        public static string TransformXmlForExplorer(string xml)
        {
            string xslt = Resources.xml;

            XslCompiledTransform transform = new XslCompiledTransform();            
            transform.Load(new XPathDocument(new StringReader(xslt)));

            XPathDocument xpathDoc = new XPathDocument(new StringReader(xml));

            StringWriter sw = new StringWriter();
            transform.Transform(xpathDoc, null, sw);

            return sw.ToString();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions