我有一个非常大而且非常复杂的xml文件,我只想从中提取非常特定的元素。我要检索的唯一元素是atcocode、naptancode、描述符中的所有元素、转换和计时状态的经度和纬度以及车站分类中的公共汽车站类型。
我知道vs可以自动生成一个类,但这将解析不必要的细节。任何帮助都将不胜感激。
最小值
XML文件的片段:
030028280001
brkpjmt
Tinkers Corner
adj Forbury Lane
Holt Lane
opp
E0053849
0
UKOS
439773
165685
-1.42979961186
51.38882190967
BCT
CUS
OTH
NW
030G58280001
064
...
例如,这是我心目中的C类课程:
class Naptan
{
public string AtcoCode { get; set; }
public string NaptanCode { get; set; }
public long Latitude { get; set; }
public long Longitude { get; set; }
public string TimmingStatus { get; set; }
public string BusStopType { get; set; }
public string CommonName { get; set; }
public string Landmark { get; set; }
public string Street { get; set; }
public string Indicator { get; set; }
}
完成
目前,我已经尝试了这种方法,将其转换为一个json文件,然后将其解析为一个类,然后手动循环遍历一个对象列表,并生成一个从原始类压缩而来的新对象列表。
编辑
我已经实现了prateek deshmukh方法,但是这并没有按照要求提取特定的元素,所以我还必须添加这个新代码,我想避免这样做,有没有人有更好的建议?以下内容:
NaPTAN tempRawData;
XmlSerializer serializer = new XmlSerializer(typeof(NaPTAN));
using (FileStream fileStream = new FileStream(@"F:\DfT1.xml", FileMode.Open))
{
tempRawData = (NaPTAN)serializer.Deserialize(fileStream);
}
foreach (var StopPoint in tempRawData.StopPoints)
{
Locations.Add(StopPoint.AtcoCode, new Naptan()
{
NaptanCode = StopPoint.NaptanCode,
Latitude = StopPoint.Place.Location.Translation.Latitude,
Longitude = StopPoint.Place.Location.Translation.Longitude,
TimmingStatus = StopPoint.StopClassification.OnStreet.Bus.TimingStatus,
BusStopType = StopPoint.StopClassification.OnStreet.Bus.BusStopType,
CommonName = StopPoint.Descriptor.CommonName,
Landmark = StopPoint.Descriptor.Landmark,
Street = StopPoint.Descriptor.Street,
Indicator = StopPoint.Descriptor.Indicator
});
}