[英]Reading/ Extracting specific elements from XML file C#
我有一個非常大且非常復雜的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; }
}
完成
目前,我已經嘗試過將其轉換為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
});
}
嘗試使用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; }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.