我有一个非常大且非常复杂的XML文件,我只想从其中提取非常具体的元素。我要检索的唯一元素是Atcocode,NaptanCode,描述符中的所有元素,“翻译”中的经度和纬度以及“停车”分类中的计时状态和公交车站类型。
我知道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文件,然后将其解析为类,然后手动循环遍历对象列表并生成从原始类压缩而来的新对象列表的方法。 / p>
编辑
我已经实现了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
});
}
答案 0 :(得分:1)
尝试使用xml linq进行以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
XNamespace ns = root.GetDefaultNamespace();
List<Naptan> atcoCodes = doc.Descendants(ns + "StopPoint").Select(x => new Naptan()
{
AtcoCode = (string)x.Element(ns + "AtcoCode"),
NaptanCode = (string)x.Element(ns + "NaptanCode"),
Latitude = (double)x.Descendants(ns + "Latitude").FirstOrDefault(),
Longitude = (double)x.Descendants(ns + "Longitude").FirstOrDefault(),
TimmingStatus = (string)x.Descendants(ns + "TimingStatus").FirstOrDefault(),
BusStopType = (string)x.Descendants(ns + "BusStopType").FirstOrDefault(),
CommonName = (string)x.Descendants(ns + "CommonName").FirstOrDefault(),
Landmark = (string)x.Descendants(ns + "Landmark").FirstOrDefault(),
Street = (string)x.Descendants(ns + "Street").FirstOrDefault(),
Indicator = (string)x.Descendants(ns + "Indicator").FirstOrDefault()
}).ToList();
}
}
class Naptan
{
public string AtcoCode { get; set; }
public string NaptanCode { get; set; }
public double Latitude { get; set; }
public double 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; }
}
}
答案 1 :(得分:-1)
您是否正在寻找这样的解决方案?请查看它已反序列化对象的类
NaPTAN result = (NaPTAN)serializer.Deserialize(fileStream);