C# 从XML文件C读取/提取特定元素#

C# 从XML文件C读取/提取特定元素#,c#,xml,xml-parsing,linq-to-xml,xmldocument,C#,Xml,Xml Parsing,Linq To Xml,Xmldocument,我有一个非常大而且非常复杂的XML文件,我只想从中提取非常特定的元素。我只想检索Atcocode、NaptanCode、描述符中的所有元素、转换和计时状态中的经度和纬度以及站点分类中的公交站点类型 我知道VS可以自动生成一个类,但这将解析不必要的细节。任何帮助都将不胜感激 最小值 XML文件中的代码段: <?xml version="1.0" encoding="utf-8"?> <NaPTAN xmlns:xsi="http://www.w3.org/2001/XMLSche

我有一个非常大而且非常复杂的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>
...
完成

目前,我尝试了这种方法,将其转换为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
                    });
                }

你在寻找这样的解决方案吗?请查看它已反序列化对象的类

NaPTAN result = (NaPTAN)serializer.Deserialize(fileStream);

尝试使用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; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
XElement根=文档根;
XNamespace ns=root.GetDefaultNamespace();
列出atcoCodes=doc.substands(ns+“StopPoint”)。选择(x=>new Naptan()
{
AtcoCode=(字符串)x.Element(ns+“AtcoCode”),
NaptanCode=(字符串)x.Element(ns+“NaptanCode”),
纬度=(双精度)x.s(ns+“纬度”).FirstOrDefault(),
经度=(双精度)x.s(ns+“经度”).FirstOrDefault(),
TimmingStatus=(字符串)x.subjects(ns+“TimingStatus”).FirstOrDefault(),
BusStopType=(字符串)x.subjects(ns+“BusStopType”).FirstOrDefault(),
CommonName=(字符串)x.subjects(ns+“CommonName”).FirstOrDefault(),
Landmark=(字符串)x.subjects(ns+“Landmark”).FirstOrDefault(),
Street=(字符串)x.s(ns+“Street”).FirstOrDefault(),
指示符=(字符串)x.s(ns+“指示符”).FirstOrDefault()
}).ToList();
}
}
纳普坦类
{
公共字符串AtcoCode{get;set;}
公共字符串NaptanCode{get;set;}
公共双纬度{get;set;}
公共双经度{get;set;}
公共字符串TimmingStatus{get;set;}
公共字符串BusStopType{get;set;}
公共字符串CommonName{get;set;}
公共字符串标记{get;set;}
公共字符串Street{get;set;}
公共字符串指示符{get;set;}
}
}

如果您能分享到目前为止您的一些尝试,那将是非常棒的。我现在尝试更新这个问题,我附上了我目前拥有的代码,但我想避免完全使用该方法。感谢您的帖子,不过我想将数据解析成一个类,并将其用作对象列表。所以我需要能够在程序中使用它们,而不仅仅是在调试视图中。@Jamestrod,我刚刚在代码中显示了数据,您可以从dataset对象访问表,并通过选择表和数据的行、列来形成类。调试视图只是显示读取的数据表。@Jamestrod,我添加了CS类并解析了整个对象,而不是使用Json。你能回顾一下你是否在寻找相同的信息吗?我已经编辑了这个问题,虽然这是可行的,但它需要手动提取信息,在解析整个文件时,这似乎浪费了很多资源。
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; }
    }
}