martes, 15 de febrero de 2011

Serializar y Deserializar clases

 using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
namespace TestStuff
{
public class Configuration
{
#region properties
public List<string> UIComponents { get; set; }
public List<string> Settings { get; set; }
#endregion
//serialize itself
public string Serialize()
{
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(Configuration));
using (StreamWriter xmlTextWriter = new StreamWriter(memoryStream))
{
xs.Serialize(xmlTextWriter, this);
xmlTextWriter.Flush();
//xmlTextWriter.Close();
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
memoryStream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(memoryStream);
return reader.ReadToEnd();
}
}
//deserialize into itself
public void Deserialize(string xmlString)
{
String XmlizedString = null;
using (MemoryStream memoryStream = new MemoryStream())
{
using (StreamWriter w = new StreamWriter(memoryStream))
{
w.Write(xmlString);
w.Flush();
XmlSerializer xs = new XmlSerializer(typeof(Configuration));
memoryStream.Seek(0, SeekOrigin.Begin);
XmlReader reader = XmlReader.Create(memoryStream);
Configuration currentConfig = (Configuration)xs.Deserialize(reader);
this.Settings = currentConfig.Settings;
this.UIComponents = currentConfig.UIComponents;
w.Close();
}
}
}
static void Main(string[] args)
{
Configuration thisConfig = new Configuration();
thisConfig.Settings = new List<string>(){
"config1", "config2"
};
thisConfig.UIComponents = new List<string>(){
"comp1", "comp2"
};
//serializing the object
string serializedString = thisConfig.Serialize();

Configuration myConfig = new Configuration();
//deserialize into myConfig object
myConfig.Deserialize(serializedString);
}
}

}

lunes, 31 de enero de 2011

Habilitar SSL en un WCF (Https)

Para habilitar el SSL en un WCF el web.config debe contener las siguientes instrucción:

<system.serviceModel>


<behaviors>    


 <endpointBehaviors>


  <behavior name="TestServiceAspNetAjaxBehavior">


   <enableWebScript />


  </behavior>


 </endpointBehaviors>


</behaviors>


<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />


<services>


 <service name="TestService">


  <endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"


   binding="webHttpBinding" bindingConfiguration="webBinding" contract="TestService" />


 </service>


</services>


 <bindings>


   <webHttpBinding>


     <binding name="webBinding">


       <security mode="Transport">


       </security>


     </binding>


   </webHttpBinding>


 </bindings>


</system.serviceModel>

lunes, 22 de noviembre de 2010

Subir o bajar el Scroll de un div con JavaScript

Esta es una función para realizarlo con animación

<script type="text/javascript">

var t;
var doLoop = true;

function scrollDivToTop(divid)
{
var div = document.getElementById(divid);

if (div.scrollTop>0 && doLoop)
{
div.scrollTop-=3;
t = setTimeout("scrollDivToTop('" + divid + "')", 1);
}
else clearTimeout(t);
}

function scrollDivToBottom(divid)
{

var div = document.getElementById(divid);

if (div.scrollTop<999999999 && doLoop)
{
div.scrollTop+=3;
t = setTimeout("scrollDivToBottom('" + divid + "')", 1);
}
else clearTimeout(t);
}

function stop()
{
doLoop = false;
setTimeout('doLoop = true;', 5);
}

</script>

Lo importante es esta parte

div.scrollTop=0; //0 para que se suba al principio del div  y 999999999 para que baje al fondo del div

Join entre DataTables en LINQ

private void algo()
{
     DataTable dtPolizas = new DataTable();
     DataTable dtServicios = new DataTable();
     var ret = from p in dtPolizas.AsEnumerable()
               join q in dtServicios.AsEnumerable() on p.Field<int>("ID") equals q.Field<int>("ID") into UP
               from q in UP.DefaultIfEmpty()
               where (q.Field<string>("estado") == "00" || q.Field<string>("estado") == "12" && q.Field<int>("estado") == Convert.ToInt32(DateTime.Now.AddDays(-90).ToString("yyyyMMdd")))
               select new
               {
                   ID = Convert.ToDateTime(p.Field<int>("ID")),
                   Type = p.Field<string>("Type"),
                   Part = q.Field<int>("Part"),
                   Quantity = q.Field<int>("Quantity")
               };
     foreach (var ins in ret)
     {
     }
}

Filtrar caracteres de una cadena con LINQ

private string ReemplazarCadena(string pOrigen, int pTamano)
{
IEnumerable stringQuery =
from ch in pOrigen
where !Char.IsNumber(ch)
select ch;
foreach (char item in stringQuery)
pOrigen = pOrigen.Remove(pOrigen.IndexOf(item),1);

pOrigen = pOrigen.PadLeft(pTamano,'0');
return pOrigen;
}

lunes, 12 de julio de 2010

Contador regresivo en JavaScript

Contador regresivo generado en JavaScript

var tiempo = 10;

function ContadorRegresivo()
{
var valor = document.getElementById('tiempo');
valor.value = tiempo;
tiempo = tiempo-1;
if (tiempo > -1)
{
setTimeout("ContadorRegresivo()", 1000);
}
}

function InicializarContador()
{
tiempo = 10;
ContadorRegresivo();
}

lunes, 5 de julio de 2010

Error con el ambiente de Visual Studio 2010

En caso de que al generar una referencia web el editor muestre este mensaje "Los componentes de enumeracion de servicios web no estan disponibles. es necesario reinstalar visual studio para agregar las referencias web a su aplicacion"

Se debe ingresar el comando devenv /resetskippkgs
 
Locations of visitors to this page