Recorrer un hashtable con foreach en c#

Aca un ejemplo de como iterar dentro de un hashtable y crear un xmlDocument de paso:

 
protected void Page_Load(object sender, EventArgs e)
{
	Hashtable xx = new Hashtable();
	xx.Add("key1", "valor1");
	xx.Add("key2", "valor2");
	xx.Add("key3", "valor3");
	xx.Add("key4", "valor4");
	XmlDocument xd = documentInfoXML("Nombre", "Descriptor", "items", xx);
	Response.Write(xd.OuterXml);
}
public static XmlDataDocument documentInfoXML(string xmlDocumentNombre,
 string xmlDocumentDescriptor, string itemName, Hashtable keysValues)
{
	XmlDataDocument xdd = new XmlDataDocument();
	XmlNode xn = xdd.CreateNode(XmlNodeType.Element, xmlDocumentNombre, "NameSpace");
	XmlAttribute xa = xdd.CreateAttribute("Descripcion");
	xa.Value = xmlDocumentDescriptor;
	xn.Attributes.Append(xa);
	xdd.AppendChild(xn);
	foreach (DictionaryEntry item in keysValues)
	{
		XmlElement nodo = xdd.CreateElement(item.Key.ToString());
		XmlText valor=xdd.CreateTextNode( item.Value.ToString());
		xdd.DocumentElement.AppendChild(nodo);
		xdd.DocumentElement.LastChild.AppendChild(valor);
	}
	return xdd;
}

La parte propiamente teniendo en cuenta el titulo es la parte del foreach, un hashtable tiene DictionaryEntry como items, tanto el key como el value son objetos.
Despues en el resto del ejemplo creo un XmlDocument con el contenido del hashtable

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.