【发布时间】:2018-12-05 02:39:22
【问题描述】:
我有一个非常大且非常复杂的 XML 文件,我只想从中提取非常具体的元素。我想检索的唯一元素是 Atcocode、NaptanCode、描述符中的所有元素、来自 Translation 的经度和纬度以及来自 Stop 分类的计时状态和公交车站类型。
我知道 VS 可以自动生成一个类,但这会解析不必要的细节。任何帮助将不胜感激。
最小
来自 XML 文件的片段:
<?xml version="1.0" encoding="utf-8"?>
<NaPTAN xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.naptan.org.uk/" CreationDateTime="2018-03-22T08:59:00" ModificationDateTime="2018-03-22T08:59:00" Modification="new" RevisionNumber="0" FileName="NaPTAN030.xml" SchemaVersion="2.1" xsi:schemaLocation="http://www.naptan.org.uk/ http://www.naptan.org.uk/schema/2.1/NaPTAN.xsd">
<StopPoints>
<StopPoint CreationDateTime="2009-07-01T16:36:00" ModificationDateTime="2015-11-03T16:19:00" Modification="revise" RevisionNumber="3" Status="active">
<AtcoCode>030028280001</AtcoCode>
<NaptanCode>brkpjmt</NaptanCode>
<Descriptor>
<CommonName>Tinkers Corner</CommonName>
<Landmark>adj Forbury Lane</Landmark>
<Street>Holt Lane</Street>
<Indicator>opp</Indicator>
</Descriptor>
<Place>
<NptgLocalityRef>E0053849</NptgLocalityRef>
<LocalityCentre>0</LocalityCentre>
<Location>
<Translation>
<GridType>UKOS</GridType>
<Easting>439773</Easting>
<Northing>165685</Northing>
<Longitude>-1.42979961186</Longitude>
<Latitude>51.38882190967</Latitude>
</Translation>
</Location>
</Place>
<StopClassification>
<StopType>BCT</StopType>
<OnStreet>
<Bus>
<BusStopType>CUS</BusStopType>
<TimingStatus>OTH</TimingStatus>
<UnmarkedPoint>
<Bearing>
<CompassPoint>NW</CompassPoint>
</Bearing>
</UnmarkedPoint>
</Bus>
</OnStreet>
</StopClassification>
<StopAreas>
<StopAreaRef CreationDateTime="2009-07-01T16:46:00" ModificationDateTime="2009-07-01T16:46:00" Modification="new" RevisionNumber="0" Status="active">030G58280001</StopAreaRef>
</StopAreas>
<AdministrativeAreaRef>064</AdministrativeAreaRef>
</StopPoint>
...
例如,这是我想到的 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; }
}
完成
Link to the whole XML file in question
目前,我尝试了将其转换为 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
});
}
【问题讨论】:
-
如果你能分享一个minimal reproducible example到目前为止你的尝试,那就太棒了。
-
我现在尝试更新问题,我已经附上了我目前拥有的代码,但我想完全避免使用该方法。
标签: c# xml xml-parsing linq-to-xml xmldocument