Monday, 16 September 2013

Linque to fetch data from XML with condition using linq

List<District> district = new List<District>();

            List<District> district2 = new List<District>();


            string XMLFile1 =HttpContext.Current.Server.MapPath("~/XMLFiles/District.xml");
            DataSet dsxml = new DataSet();
            // Create new FileStream with which to read the schema.
            System.IO.FileStream fsReadXml = new System.IO.FileStream
                (XMLFile1, System.IO.FileMode.Open);
            try
            {
                dsxml.ReadXml(fsReadXml);

            }
            catch (Exception ex)
            {
                string e = ex.Message;
            }
            finally
            {
                fsReadXml.Close();
            }


            for (int i = 0; i < dsxml.Tables[0].Rows.Count; i++)
            {

                district.Add(new District
                {
                    districtId = (dsxml.Tables[0].Rows[i][0].ToString()),
                    districtName = (dsxml.Tables[0].Rows[i][1].ToString()),
                    stateName = (dsxml.Tables[0].Rows[i][2].ToString()),
                    stateId = (dsxml.Tables[0].Rows[i][3].ToString())
                });
            }
            // Create the query.
            var dist = (from d in district
                                   where d.stateId == "1"
                                   select new{ d.districtId,d.districtName, d.stateId,d.stateName });
            foreach (var d in dist)
            {
              
                district2.Add(new District
                {
                    districtId = d.districtId,
                    districtName = d.districtName,
                    stateName = d.stateName,
                    stateId = d.stateId
                });
            }

            return district2;

No comments:

Post a Comment