C# 读取最大为20MB的xml,并将其分配给类列表

C# 读取最大为20MB的xml,并将其分配给类列表,c#,xml,C#,Xml,我有一个高达20 mb的xml: <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.

我有一个高达20 mb的xml:

<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>
我想以最快的方式创建一个列表。 提前感谢。

您应该搜索类用法。这是以只读方式访问XML文件的最快方法,尤其是当文件很大时。它一次读取一个元素,所以您不需要一次加载整个文档


您还可以将LINQ检查为XML。它对开发人员更友好,但不幸的是需要将整个XML加载到内存中,当文件较大时,这可能会导致问题。

请看一看,这会起作用,但不确定这是否是最快的方法。

这正是您想要的

 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();

将给您一个列表。

reader=new-XmlTextReader\u XMLUrlPath+indexneighbourth.xml; 边读边读 { 如果reader.NodeType==XmlNodeType.Element&&reader.Name==Code { HotelNeighbority HotelNeighbority=新HotelNeighbority; hotelneighbority.LocationID=int.Parsereader.GetAttributeLocationID.ToString; hotelneighborhoodname.ToString; HotelNeighboration.Latitude=reader.GetAttributeRelativity.ToString!=string.Empty?float.Parsereader.GetAttributeRelativity.ToString:0; hotelneighbority.Longitude=reader.GetAttributeLongitude.ToString!=string.Empty?float.Parsereader.GetAttributeLongitude.ToString:0; LstHNeighbourHood.AddhotelNeighbourhood; } } 读者。关闭; reader=null;
path

下次,请对格式更为敏感。有关您编写的代码问题的问题必须描述具体问题,并包括重现该问题的有效代码。请参阅以获取指导。请阅读:和+1。我怎么能忘记序列化…总比忘记Dre好。
 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();