Buscar en un xml
------------
XmlDocument xDoc = new XmlDocument();
//La ruta del documento XML permite rutas relativas
string url= Server.MapPath("XML/busqueda.xml");
xDoc.Load(url);
XmlNodeList personas = xDoc.GetElementsByTagName("busqueda");
XmlNodeList lista = ((XmlElement)personas[0]).GetElementsByTagName("item");
StringBuilder Resultado = new StringBuilder();
Resultado.AppendFormat("");
string strAno= bdlAno.SelectedValue;
string strRegion =bdlRegion.SelectedValue;
string strcomponente =dblComponente.SelectedValue;
string strDepartamento =dblDepartamento.SelectedValue;
string strMunicipios =dblMunicipios.SelectedValue;
string strLineaProductiva =dblLineaProductiva.SelectedValue;
string strTipo =dblTipo.SelectedValue;
string strDescripcion = txtDescripcion.Text;
foreach (XmlElement nodo in lista)
{
int i=0;
XmlNodeList xmlAno = nodo.GetElementsByTagName("ano");
XmlNodeList xmlRegion = nodo.GetElementsByTagName("region");
XmlNodeList xmlComponente = nodo.GetElementsByTagName("componente");
XmlNodeList xmlDepartamento = nodo.GetElementsByTagName("departamento");
XmlNodeList xmlMunicipio = nodo.GetElementsByTagName("municipio");
XmlNodeList xmlLinreaProductiva = nodo.GetElementsByTagName("linreaProductiva");
XmlNodeList xmlTipo = nodo.GetElementsByTagName("tipo");
XmlNodeList xmlDescripcion = nodo.GetElementsByTagName("descripcion");
XmlNodeList xmlUrlFile = nodo.GetElementsByTagName("url");
string urlFiles = ConfigurationSettings.AppSettings["urlHost"] + "/Documentos/" + xmlUrlFile[i].InnerText;
if (xmlAno[i].InnerText == strAno || strRegion == xmlRegion[i].InnerText || strcomponente == xmlComponente[i].InnerText || strDepartamento == xmlDepartamento[i].InnerText || strMunicipios == xmlMunicipio[i].InnerText || strLineaProductiva == xmlLinreaProductiva[i].InnerText || strTipo == xmlTipo[i].InnerText || xmlDescripcion.ToString().IndexOf(strDescripcion)==-1)
{
Resultado.AppendFormat("");
Resultado.AppendFormat("", xmlDescripcion[i].InnerText);
Resultado.AppendFormat("");
Resultado.AppendFormat("");
Resultado.AppendFormat("", urlFiles, xmlUrlFile[i].InnerText);
Resultado.AppendFormat("");
}
}
Resultado.AppendFormat("
");
lbResultado.Text = Resultado.ToString();
XmlDocument xDoc = new XmlDocument();
//La ruta del documento XML permite rutas relativas
string url= Server.MapPath("XML/busqueda.xml");
xDoc.Load(url);
XmlNodeList personas = xDoc.GetElementsByTagName("busqueda");
XmlNodeList lista = ((XmlElement)personas[0]).GetElementsByTagName("item");
StringBuilder Resultado = new StringBuilder();
Resultado.AppendFormat("");
string strAno= bdlAno.SelectedValue;
string strRegion =bdlRegion.SelectedValue;
string strcomponente =dblComponente.SelectedValue;
string strDepartamento =dblDepartamento.SelectedValue;
string strMunicipios =dblMunicipios.SelectedValue;
string strLineaProductiva =dblLineaProductiva.SelectedValue;
string strTipo =dblTipo.SelectedValue;
string strDescripcion = txtDescripcion.Text;
foreach (XmlElement nodo in lista)
{
int i=0;
XmlNodeList xmlAno = nodo.GetElementsByTagName("ano");
XmlNodeList xmlRegion = nodo.GetElementsByTagName("region");
XmlNodeList xmlComponente = nodo.GetElementsByTagName("componente");
XmlNodeList xmlDepartamento = nodo.GetElementsByTagName("departamento");
XmlNodeList xmlMunicipio = nodo.GetElementsByTagName("municipio");
XmlNodeList xmlLinreaProductiva = nodo.GetElementsByTagName("linreaProductiva");
XmlNodeList xmlTipo = nodo.GetElementsByTagName("tipo");
XmlNodeList xmlDescripcion = nodo.GetElementsByTagName("descripcion");
XmlNodeList xmlUrlFile = nodo.GetElementsByTagName("url");
string urlFiles = ConfigurationSettings.AppSettings["urlHost"] + "/Documentos/" + xmlUrlFile[i].InnerText;
if (xmlAno[i].InnerText == strAno || strRegion == xmlRegion[i].InnerText || strcomponente == xmlComponente[i].InnerText || strDepartamento == xmlDepartamento[i].InnerText || strMunicipios == xmlMunicipio[i].InnerText || strLineaProductiva == xmlLinreaProductiva[i].InnerText || strTipo == xmlTipo[i].InnerText || xmlDescripcion.ToString().IndexOf(strDescripcion)==-1)
{
Resultado.AppendFormat("");
Resultado.AppendFormat("", xmlDescripcion[i].InnerText);
Resultado.AppendFormat("");
Resultado.AppendFormat("");
Resultado.AppendFormat("", urlFiles, xmlUrlFile[i].InnerText);
Resultado.AppendFormat("");
}
}
Resultado.AppendFormat("
Descripcion: {0} |
Ruta Archivo: {1} |
lbResultado.Text = Resultado.ToString();
Comentarios