Search This Blog

Thursday, July 2, 2009

Read Child Node From a XML and Bind a DropDown List By this

My xml file is Like that

Root Node := Weather_Data
Parent Nade : = Weather

Child Node Under Parent "Weather"
ChildNode : City
ChildNode :Weather

public ArrayList ReadValue(string tag, string key)
{
string _file = "Weather.xml";
ArrayList arr = new ArrayList();
string _path = Server.MapPath("~/");
//load the document into a stream
FileStream stream = new FileStream(_path + _file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlDocument doc = new XmlDocument();
//load the stream into an XML Document
doc.Load(stream);
//variable to hold the value
string returnValue = string.Empty;
//get all the elements for the specified tag
XmlNodeList nodeList = doc.GetElementsByTagName(tag);
//loop through the document
for (int i = 0; i < nodeList.Count; i++)
{
for (int j = 0; j < nodeList[i].ChildNodes.Count; j++)
{
//check to see if we have a match
if (nodeList[i].ChildNodes[j].Name == key)
{
returnValue = nodeList[i].ChildNodes[j].InnerText;
arr.Add(nodeList[i].ChildNodes[j].InnerText);
//break;
}
}
}
return arr;
}

Call the above method for bind your DropdownList Like

ArrayList arrList= ReadValue("Weather", "City");
DropDownList1.DataSource =
arrList;
DropDownList1.DataBind();



Hope This will help you

1 comment:

Johnson said...

really effective.but i think you put something extra code over there.