Archive for Maggio 1st, 2008

.NET - XSLT on XML

CodeKeep C# Feed Maggio 1st, 2008

Description: How to xsl transform xml in .NET

Link: http://www.codekeep.net/snippets/943f0858-d671-4e48-8cef-4064dba6dcd7.aspx

using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;

//Create XPathDocument from the source string passed in.
			StringReader SourceData = new StringReader(xml);
			XPathDocument XmlSourceData = new XPathDocument(SourceData);
			SourceData.Close();

			//Create XPathDocument for stylesheet from the above string
			StringReader StyleSheetData = new StringReader(Sb.ToString());
			XPathDocument XslMasterDoc = new XPathDocument(StyleSheetData);
			StyleSheetData.Close();

			//Create a navigator for the source document. There is information we need
			//to extract from it before we do the transform.
			XPathNavigator XmlSourceNav = XmlSourceData.CreateNavigator();

			//Create transform object
			XslTransform XslDoc = new XslTransform();

			//Create stream to fill with the transformed output
			MemoryStream ms = new MemoryStream();

			//Load the xslt and do the transform
			XmlUrlResolver resolver = new XmlUrlResolver();
			XslDoc.Load(XslMasterDoc, resolver, Assembly.GetExecutingAssembly().Evidence);
			XslDoc.Transform(XmlSourceData, null, ms, resolver);

			StreamReader sr = new StreamReader(ms);
			ms.Seek(0, SeekOrigin.Begin);
			string returnValue = sr.ReadToEnd();

			return System.Web.HttpContext.Current.Server.HtmlDecode( returnValue );

.NET - Base64 Encoding/Decoding

CodeKeep C# Feed Maggio 1st, 2008

Description: How to encode and decode string with base64 in .NET/C#.

Link: http://www.codekeep.net/snippets/ebcc3df1-4ccb-466c-993b-fd8b8441b3ed.aspx

		public string Base64Encode(string data)
		{
			byte[] encData_byte = new byte[data.Length];
			encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
			string encodedData = Convert.ToBase64String(encData_byte);
			return encodedData;
		}

		public string Base64Decode(string data)
		{
			System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
			System.Text.Decoder utf8Decode = encoder.GetDecoder();

			byte[] todecode_byte = Convert.FromBase64String(data);
			int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
			char[] decoded_char = new char[charCount];
			utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
			string result = new String(decoded_char);
			return result;

		}