Click here to Skip to main content
15,897,518 members
Articles / Programming Languages / XSLT

Customize Applications with XML Fragments: Part 2

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
19 Jun 20073 min read 22K   111   9  
An advanced discussion of customizing applications with 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).TrimEnd('\0');
                }
            }
        }
        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.


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