-3

I have an xml which is upto 20 mb:

<Neighborhood>
<Code LocationID='27767' NeighborhoodName='Broadway-Times Square' Latitude='40.7586' Longitude='-73.988152'/>
<Code LocationID='27767' NeighborhoodName='Midtown East' Latitude='40.755645' Longitude='-73.967428'/>
</Neighborhood>

I want to read the xml and create a list of class.

The class is as follows :

public class HotelNeighbourhood
{
  public int LocationID { get; set; }
  public string NeighborhoodName { get; set; }
  public float Latitude { get; set; }
  public float Longitude { get; set; }
}

I want create a list in a fastest possible manner. Thanks in advance.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
palak mehta
  • 696
  • 4
  • 13
  • 30
  • Next time, please be more sensitive about formatting. – Soner Gönül Jul 10 '13 at 08:51
  • 2
    Questions concerning problems with code you've written must describe the specific problem and include valid code to reproduce it. See [SSCCE.org](http://sscce.org/) for guidance. – DGibbs Jul 10 '13 at 08:52
  • please read : http://stackoverflow.com/questions/12931769/c-sharp-and-reading-large-xml-files and http://stackoverflow.com/questions/7671958/reading-large-xml-documents-in-net and http://stackoverflow.com/questions/468948/in-c-sharp-what-is-the-best-way-to-parse-large-xml-size-of-1gb – Sabilv Jul 10 '13 at 09:00

4 Answers4

1

Have a look at this, this will work whereas not sure if it is the fastest approach.

Community
  • 1
  • 1
Vinay Pandey
  • 8,589
  • 9
  • 36
  • 54
0

You should search for XmlReader class usage. It's the fastest way to gain read-only access to XML file, especially when the file is big. It reads one element at the time, so you don't need to load whole document at once.

You could also check LINQ to XML. It's more developer-friendly, but unfortunately requires loading whole XML into memory, what could cause problem when file is large.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0

This does what you want

 var query = (from n in xml.Descendants("Code")
              select new HotelNeighbourhood()
              {
                   LocationID = Convert.ToInt32(n.Attribute("LocationID").Value),
                   NeighborhoodName = n.Attribute("NeighborhoodName").Value,
                   Longitude = float.Parse(n.Attribute("Longitude").Value),
                   Latitude = float.Parse(n.Attribute("Latitude").Value)

              }).ToList();

Will give you a List<HotelNeighborhood>.

DGibbs
  • 14,316
  • 7
  • 44
  • 83
0

reader = new XmlTextReader(_XMLUrlPath + "indexNeighbourhood.xml"); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "Code") { HotelNeighbourhood hotelNeighbourhood = new HotelNeighbourhood(); hotelNeighbourhood.LocationID = int.Parse(reader.GetAttribute("LocationID").ToString()); hotelNeighbourhood.NeighborhoodName = reader.GetAttribute("NeighborhoodName").ToString(); hotelNeighbourhood.Latitude = reader.GetAttribute("Latitude").ToString() != string.Empty ? float.Parse(reader.GetAttribute("Latitude").ToString()) : 0; hotelNeighbourhood.Longitude = reader.GetAttribute("Longitude").ToString() != string.Empty ? float.Parse(reader.GetAttribute("Longitude").ToString()) : 0; LstHNeighbourHood.Add(hotelNeighbourhood); } } reader.Close(); reader = null; path

palak mehta
  • 696
  • 4
  • 13
  • 30