< ?xml version="1.0" encoding="utf-8" ? >
< Cities >
< City StationName="Mumbai" >
< WeatherCondition >Not Extreem< /WeatherCondition >
< WindDirection >North-to-South< /WindDirection >
< Temperature >30 degree C< /Temperature >
< DewPoint >16< /DewPoint >
< Humidity >14.05< /Humidity >
< WindSpeed >Mild< /WindSpeed >
< /City >
< City StationName="Kolkata" >
< WeatherCondition >Dry< /WeatherCondition >
< WindDirection >East-to-South< /WindDirection >
< Temperature >32 degree C< /Temperature >
< DewPoint >11< /DewPoint >
< Humidity >12.35< /Humidity >
< WindSpeed >Strong< /WindSpeed >
< /City >
< City StationName="Delhi" >
< WeatherCondition >Hot< /WeatherCondition >
< WindDirection >East-to-North< /WindDirection >
< Temperature >42 degree C< /Temperature >
< DewPoint >7< /DewPoint >
< Humidity >11.35< /Humidity >
< WindSpeed >Strong< /WindSpeed >
< /City >
< /Cities >
Now read the above xml data using Xml.Linq.XDocument
public static List
{
// Load a Xml File into XDocument class
XDocument xmlDoc = XDocument.Load(@"App_Data/CitiesClimate.xml");
// Read the Xml data into a
var cities = from xmlData in xmlDoc.Elements("Cities").Elements("City")
select xmlData;
Climate climate = null;
List
//Now loop through all the cities in resultset
foreach (var city in cities)
{
climate = new Climate();
climate.StationName = city.Attribute("StationName").Value;
climate.WeatherCondition = city.Element("WeatherCondition").Value;
climate.WindDirection = city.Element("WindDirection").Value;
climate.Temperature = city.Element("Temperature").Value;
climate.DewPoint = Convert.ToDecimal(city.Element("DewPoint").Value) ;
climate.Humidity =Convert.ToDecimal(city.Element("Humidity").Value);
climate.WindSpeed = city.Element("WindSpeed").Value;
climateList.Add(climate);
}
return climateList;
}
Now see how to search some records
// Here we can perform search
var cities1 = from xmlData in xmlDoc.Elements("Cities").Elements("City")
where Convert.ToInt32(xmlData.Elements("DewPoint")) > 11
select xmlData;
No comments:
Post a Comment