我运行此代码https://gist.github.com/ccumaya/10b1a58efe06c1f8080045457c5cf4b8https://dotnetfiddle.net/ (目前该网站无法取得联系,所以我使用的依据。)

我的目的是在XML文件中获取GPS点,但是XPath

'/gpx/trk/trkseg/trkpt' 

不起作用。 我不知道哪里出了问题,但我正在使用XPath

'//*' 

并获得1439个结果。 我需要帮助,谢谢您的指导。

xml结构如下。

<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpxtrkx="http://www.garmin.com/xmlschemas/TrackStatsExtension/v1" xmlns:wptx1="http://www.garmin.com/xmlschemas/WaypointExtension/v1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creator="GPSMAP 64ST TWN" version="1.1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackStatsExtension/v1 http://www8.garmin.com/xmlschemas/TrackStatsExtension.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">
  <metadata>
    <link href="http://www.garmin.com">
      <text>Garmin International</text>
    </link>
    <time>2018-10-05T09:21:31Z</time>
  </metadata>
  <trk>
    <name>2018-10-05 17:21:26</name>
    <extensions>
      <gpxx:TrackExtension>
        <gpxx:DisplayColor>Cyan</gpxx:DisplayColor>
      </gpxx:TrackExtension>
      <gpxtrkx:TrackStatsExtension>
        <gpxtrkx:Distance>1033</gpxtrkx:Distance>
        <gpxtrkx:TotalElapsedTime>996</gpxtrkx:TotalElapsedTime>
        <gpxtrkx:MovingTime>870</gpxtrkx:MovingTime>
        <gpxtrkx:StoppedTime>86</gpxtrkx:StoppedTime>
        <gpxtrkx:MovingSpeed>1</gpxtrkx:MovingSpeed>
        <gpxtrkx:MaxSpeed>2</gpxtrkx:MaxSpeed>
        <gpxtrkx:MaxElevation>207</gpxtrkx:MaxElevation>
        <gpxtrkx:MinElevation>189</gpxtrkx:MinElevation>
        <gpxtrkx:Ascent>17</gpxtrkx:Ascent>
        <gpxtrkx:Descent>5</gpxtrkx:Descent>
        <gpxtrkx:AvgAscentRate>0</gpxtrkx:AvgAscentRate>
        <gpxtrkx:MaxAscentRate>0</gpxtrkx:MaxAscentRate>
        <gpxtrkx:AvgDescentRate>0</gpxtrkx:AvgDescentRate>
        <gpxtrkx:MaxDescentRate>-0</gpxtrkx:MaxDescentRate>
        </gpxtrkx:TrackStatsExtension>
      </extensions>
      <trkseg>
        <trkpt lat="25.0312615000" lon="121.3505846635">
        <ele>189.04</ele>
        <time>2018-10-05T09:04:55Z</time>
      </trkpt>
      <trkpt lat="25.0312520284" lon="121.3505897764">
        <ele>189.04</ele>
        <time>2018-10-05T09:04:57Z</time>
        </trkpt>
      <trkpt lat="25.0312457420" lon="121.3506018464">
        <ele>196.43</ele>
        <time>2018-10-05T09:04:59Z</time>
      </trkpt>
      <trkpt lat="25.0312426407" lon="121.3506035227">
        <ele>196.42</ele>
        <time>2018-10-05T09:05:01Z</time>
      </trkpt>
    </trkseg>
  </trk>
</gpx>

谢谢你的帮助

#1楼 票数:0

当您有很多要解析的xml元素并且有可用的架构时,在大多数情况下,最好反序列化以解析数据。

因此,我进入了您的xml中的架构网站( http://www.topografix.com/GPX/1/1 ),并通过将各节以及一些附加编辑粘贴到标题中来创建下面的架构,以获得正确的结果。 大多数情况下,网站具有完整的架构。 该网站没有。

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema id="XMLSchema1"
    targetNamespace="http://www.topografix.com/GPX/1/1"
    elementFormDefault="qualified"
    xmlns="http://www.topografix.com/GPX/1/1"
    xmlns:xml="http://www.w3.org/XML/1998/namespace"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:element name="gpx" type="gpxType"/>
  <xsd:complexType name="metadataType">
    <xsd:sequence>
      <!-- elements must appear in this order -->
      <xsd:element name="name" type="xsd:string" minOccurs="0"/>
      <xsd:element name="desc" type="xsd:string" minOccurs="0"/>
      <xsd:element name="author" type="personType" minOccurs="0"/>
      <xsd:element name="copyright" type="copyrightType" minOccurs="0"/>
      <xsd:element name="link" type="linkType" minOccurs="0" maxOccurs="unbounded"/>
      <xsd:element name="time" type="xsd:dateTime" minOccurs="0"/>
      <xsd:element name="keywords" type="xsd:string" minOccurs="0"/>
      <xsd:element name="bounds" type="boundsType" minOccurs="0"/>
      <xsd:element name="extensions" type="extensionsType" minOccurs="0"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="gpxType">
    <xsd:sequence>
      <xsd:element name="metadata" type="metadataType" minOccurs="0"/>
      <xsd:element name="wpt" type="wptType" minOccurs="0" maxOccurs="unbounded"/>
      <xsd:element name="rte" type="rteType" minOccurs="0" maxOccurs="unbounded"/>
      <xsd:element name="trk" type="trkType" minOccurs="0" maxOccurs="unbounded"/>
      <xsd:element name="extensions" type="extensionsType" minOccurs="0"/>
    </xsd:sequence>
    <xsd:attribute name="version" type="xsd:string" use="required" fixed="1.1"/>
    <xsd:attribute name="creator" type="xsd:string" use="required"/>
  </xsd:complexType>
  <xsd:complexType name="wptType">
    <xsd:sequence>
      <!-- elements must appear in this order -->
      <!-- Position info -->
      <xsd:element name="ele" type="xsd:decimal" minOccurs="0"/>
      <xsd:element name="time" type="xsd:dateTime" minOccurs="0"/>
      <xsd:element name="magvar" type="degreesType" minOccurs="0"/>
      <xsd:element name="geoidheight" type="xsd:decimal" minOccurs="0"/>
      <!-- Description info -->
      <xsd:element name="name" type="xsd:string" minOccurs="0"/>
      <xsd:element name="cmt" type="xsd:string" minOccurs="0"/>
      <xsd:element name="desc" type="xsd:string" minOccurs="0"/>
      <xsd:element name="src" type="xsd:string" minOccurs="0"/>
      <xsd:element name="link" type="linkType" minOccurs="0" maxOccurs="unbounded"/>
      <xsd:element name="sym" type="xsd:string" minOccurs="0"/>
      <xsd:element name="type" type="xsd:string" minOccurs="0"/>
      <!-- Accuracy info -->
      <xsd:element name="fix" type="fixType" minOccurs="0"/>
      <xsd:element name="sat" type="xsd:nonNegativeInteger" minOccurs="0"/>
      <xsd:element name="hdop" type="xsd:decimal" minOccurs="0"/>
      <xsd:element name="vdop" type="xsd:decimal" minOccurs="0"/>
      <xsd:element name="pdop" type="xsd:decimal" minOccurs="0"/>
      <xsd:element name="ageofdgpsdata" type="xsd:decimal" minOccurs="0"/>
      <xsd:element name="dgpsid" type="dgpsStationType" minOccurs="0"/>
      <xsd:element name="extensions" type="extensionsType" minOccurs="0"/>
    </xsd:sequence>
    <xsd:attribute name="lat" type="latitudeType" use="required"/>
    <xsd:attribute name="lon" type="longitudeType" use="required"/>
  </xsd:complexType>
  <xsd:complexType name="rteType">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string" minOccurs="0"/>
      <xsd:element name="cmt" type="xsd:string" minOccurs="0"/>
      <xsd:element name="desc" type="xsd:string" minOccurs="0"/>
      <xsd:element name="src" type="xsd:string" minOccurs="0"/>
      <xsd:element name="link" type="linkType" minOccurs="0" maxOccurs="unbounded"/>
      <xsd:element name="number" type="xsd:nonNegativeInteger" minOccurs="0"/>
      <xsd:element name="type" type="xsd:string" minOccurs="0"/>
      <xsd:element name="extensions" type="extensionsType" minOccurs="0"/>
      <xsd:element name="rtept" type="wptType" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="trkType">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string" minOccurs="0"/>
      <xsd:element name="cmt" type="xsd:string" minOccurs="0"/>
      <xsd:element name="desc" type="xsd:string" minOccurs="0"/>
      <xsd:element name="src" type="xsd:string" minOccurs="0"/>
      <xsd:element name="link" type="linkType" minOccurs="0" maxOccurs="unbounded"/>
      <xsd:element name="number" type="xsd:nonNegativeInteger" minOccurs="0"/>
      <xsd:element name="type" type="xsd:string" minOccurs="0"/>
      <xsd:element name="extensions" type="extensionsType" minOccurs="0"/>
      <xsd:element name="trkseg" type="trksegType" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="extensionsType">
    <xsd:sequence>
      <xsd:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="trksegType">
    <xsd:sequence>
      <!-- elements must appear in this order -->
      <xsd:element name="trkpt" type="wptType" minOccurs="0" maxOccurs="unbounded"/>
      <xsd:element name="extensions" type="extensionsType" minOccurs="0"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="copyrightType">
    <xsd:sequence>
      <!-- elements must appear in this order -->
      <xsd:element name="year" type="xsd:gYear" minOccurs="0"/>
      <xsd:element name="license" type="xsd:anyURI" minOccurs="0"/>
    </xsd:sequence>
    <xsd:attribute name="author" type="xsd:string" use="required"/>
  </xsd:complexType>
  <xsd:complexType name="linkType">
    <xsd:sequence>
      <!-- elements must appear in this order -->
      <xsd:element name="text" type="xsd:string" minOccurs="0"/>
      <xsd:element name="type" type="xsd:string" minOccurs="0"/>
    </xsd:sequence>
    <xsd:attribute name="href" type="xsd:anyURI" use="required"/>
  </xsd:complexType>
  <xsd:complexType name="emailType">
    <xsd:attribute name="id" type="xsd:string" use="required"/>
    <xsd:attribute name="domain" type="xsd:string" use="required"/>
  </xsd:complexType>
  <xsd:complexType name="personType">
    <xsd:sequence>
      <!-- elements must appear in this order -->
      <xsd:element name="name" type="xsd:string" minOccurs="0"/>
      <xsd:element name="email" type="emailType" minOccurs="0"/>
      <xsd:element name="link" type="linkType" minOccurs="0"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="ptType">
    <xsd:sequence>
      <!-- elements must appear in this order -->
      <xsd:element name="ele" type="xsd:decimal" minOccurs="0"/>
      <xsd:element name="time" type="xsd:dateTime" minOccurs="0"/>
    </xsd:sequence>
    <xsd:attribute name="lat" type="latitudeType" use="required"/>
    <xsd:attribute name="lon" type="longitudeType" use="required"/>
  </xsd:complexType>
  <xsd:complexType name="ptsegType">
    <xsd:sequence>
      <!-- elements must appear in this order -->
      <xsd:element name="pt" type="ptType" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="boundsType">
    <xsd:attribute name="minlat" type="latitudeType" use="required"/>
    <xsd:attribute name="minlon" type="longitudeType" use="required"/>
    <xsd:attribute name="maxlat" type="latitudeType" use="required"/>
    <xsd:attribute name="maxlon" type="longitudeType" use="required"/>
  </xsd:complexType>
  <xsd:simpleType name="latitudeType">
    <xsd:restriction base="xsd:decimal">
      <xsd:minInclusive value="-90.0"/>
      <xsd:maxInclusive value="90.0"/>
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:simpleType name="longitudeType">
    <xsd:restriction base="xsd:decimal">
      <xsd:minInclusive value="-180.0"/>
      <xsd:maxExclusive value="180.0"/>
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:simpleType name="degreesType">
    <xsd:restriction base="xsd:decimal">
      <xsd:minInclusive value="0.0"/>
      <xsd:maxExclusive value="360.0"/>
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:simpleType name="fixType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="none"/>
      <xsd:enumeration value="2d"/>
      <xsd:enumeration value="3d"/>
      <xsd:enumeration value="dgps"/>
      <xsd:enumeration value="pps"/>
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:simpleType name="dgpsStationType">
    <xsd:restriction base="xsd:integer">
      <xsd:minInclusive value="0"/>
      <xsd:maxInclusive value="1023"/>
    </xsd:restriction>
  </xsd:simpleType>


</xsd:schema>

然后,我使用了msdn xsd工具( https://www.microsoft.com/zh-cn/download/details.aspx?id=19988 )。 我从cmd.exe> xsd.exe / c / l:cs gps.xsd使用了以下命令行

然后,我将xsd工具中的cs代码粘贴到我的项目中,并在第二个答案中使用以下代码反序列化

#2楼 票数:0

这是代码。 请参阅第二个答案,以了解如何生成代码。 1439结果来自OP在github链接中变量'sss'中提供的完整xml文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\gps.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(gpxType));
            gpxType gps = (gpxType)serializer.Deserialize(reader);

            trksegType[] seg = gps.trk[0].trkseg;
            wptType[] wpt = seg[0].trkpt;
            var points = wpt.Select(x => new { lat = x.lat, lon = x.lon, elevation = x.ele, time = x.time}).ToList();

        }
    }
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:2.0.50727.6421
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    // This source code was auto-generated by xsd, Version=2.0.50727.3038.
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    [System.Xml.Serialization.XmlRootAttribute("gpx", Namespace="http://www.topografix.com/GPX/1/1", IsNullable=false)]
    public partial class gpxType {
        private metadataType metadataField;
        private wptType[] wptField;
        private rteType[] rteField;
        private trkType[] trkField;
        private extensionsType extensionsField;
        private string versionField;
        private string creatorField;
        public gpxType() {
            this.versionField = "1.1";
        }
        /// <remarks/>
        public metadataType metadata {
            get {
                return this.metadataField;
            }
            set {
                this.metadataField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("wpt")]
        public wptType[] wpt {
            get {
                return this.wptField;
            }
            set {
                this.wptField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("rte")]
        public rteType[] rte {
            get {
                return this.rteField;
            }
            set {
                this.rteField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("trk")]
        public trkType[] trk {
            get {
                return this.trkField;
            }
            set {
                this.trkField = value;
            }
        }
        /// <remarks/>
        public extensionsType extensions {
            get {
                return this.extensionsField;
            }
            set {
                this.extensionsField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string version {
            get {
                return this.versionField;
            }
            set {
                this.versionField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string creator {
            get {
                return this.creatorField;
            }
            set {
                this.creatorField = value;
            }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    public partial class metadataType {
        private string nameField;
        private string descField;
        private personType authorField;
        private copyrightType copyrightField;
        private linkType[] linkField;
        private System.DateTime timeField;
        private bool timeFieldSpecified;
        private string keywordsField;
        private boundsType boundsField;
        private extensionsType extensionsField;
        /// <remarks/>
        public string name {
            get { return this.nameField; }
            set { this.nameField = value; }
        }
        public string desc {
            get { return this.descField; }
            set { this.descField = value; }
        }
        public personType author {
            get { return this.authorField; }
            set { this.authorField = value; }
        }
        public copyrightType copyright {
            get { return this.copyrightField; }
            set { this.copyrightField = value; }
        }
        [System.Xml.Serialization.XmlElementAttribute("link")]
        public linkType[] link {
            get { return this.linkField; }
            set { this.linkField = value; }
        }
        public System.DateTime time {
            get { return this.timeField; }
            set { this.timeField = value; }
        }
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool timeSpecified {
            get { return this.timeFieldSpecified; }
            set { this.timeFieldSpecified = value; }
        }
        public string keywords {
            get { return this.keywordsField; }
            set { this.keywordsField = value; }
        }
        public boundsType bounds {
            get { return this.boundsField; }
            set { this.boundsField = value; }
        }
        public extensionsType extensions {
            get { return this.extensionsField; }
            set { this.extensionsField = value; }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    public partial class personType {
        private string nameField;
        private emailType emailField;
        private linkType linkField;
        /// <remarks/>
        public string name {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }
        /// <remarks/>
        public emailType email {
            get {
                return this.emailField;
            }
            set {
                this.emailField = value;
            }
        }
        /// <remarks/>
        public linkType link {
            get {
                return this.linkField;
            }
            set {
                this.linkField = value;
            }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    public partial class emailType {
        private string idField;
        private string domainField;
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string id {
            get { return this.idField; }
            set { this.idField = value; }
        }
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string domain {
            get { return this.domainField; }
            set { this.domainField = value; }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    public partial class trksegType {
        private wptType[] trkptField;
        private extensionsType extensionsField;
        [System.Xml.Serialization.XmlElementAttribute("trkpt")]
        public wptType[] trkpt {
            get { return this.trkptField; }
            set { this.trkptField = value; }
        }
        public extensionsType extensions {
            get { return this.extensionsField; }
            set { this.extensionsField = value; }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    public partial class wptType {
        private decimal eleField;
        private bool eleFieldSpecified;
        private System.DateTime timeField;
        private bool timeFieldSpecified;
        private decimal magvarField;
        private bool magvarFieldSpecified;
        private decimal geoidheightField;
        private bool geoidheightFieldSpecified;
        private string nameField;
        private string cmtField;
        private string descField;
        private string srcField;
        private linkType[] linkField;
        private string symField;
        private string typeField;
        private fixType fixField;
        private bool fixFieldSpecified;
        private string satField;
        private decimal hdopField;
        private bool hdopFieldSpecified;
        private decimal vdopField;
        private bool vdopFieldSpecified;
        private decimal pdopField;
        private bool pdopFieldSpecified;
        private decimal ageofdgpsdataField;
        private bool ageofdgpsdataFieldSpecified;
        private string dgpsidField;
        private extensionsType extensionsField;
        private decimal latField;
        private decimal lonField;
        public decimal ele {
            get { return this.eleField; }
            set { this.eleField = value; }
        }
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool eleSpecified {
            get { return this.eleFieldSpecified; }
            set { this.eleFieldSpecified = value; }
        }
        public System.DateTime time {
            get { return this.timeField; }
            set { this.timeField = value; }
        }
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool timeSpecified {
            get { return this.timeFieldSpecified; }
            set { this.timeFieldSpecified = value; }
        }
        public decimal magvar
        {
            get { return this.magvarField; }
            set { this.magvarField = value; }
        }
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool magvarSpecified {
            get { return this.magvarFieldSpecified; }
            set { this.magvarFieldSpecified = value; }
        }
        public decimal geoidheight {
            get { return this.geoidheightField; }
            set { this.geoidheightField = value; }
        }
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool geoidheightSpecified {
            get { return this.geoidheightFieldSpecified; }
            set { this.geoidheightFieldSpecified = value; }
        }
        public string name {
            get { return this.nameField; }
            set { this.nameField = value; }
        }
        public string cmt
        {
            get { return this.cmtField; }
            set { this.cmtField = value; }
        }
        public string desc {
            get { return this.descField; }
            set { this.descField = value; }
        }
        public string src {
            get { return this.srcField; }
            set { this.srcField = value; }
        }
        [System.Xml.Serialization.XmlElementAttribute("link")]
        public linkType[] link {
            get { return this.linkField; }
            set { this.linkField = value; }
        }
        public string sym
        {
            get { return this.symField; }
            set { this.symField = value; }
        }
        public string type {
            get { return this.typeField; }
            set { this.typeField = value; }
        }
        public fixType fix {
            get { return this.fixField; }
            set { this.fixField = value; }
        }
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool fixSpecified {
            get { return this.fixFieldSpecified; }
            set { this.fixFieldSpecified = value; }
        }
        [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")]
        public string sat {
            get { return this.satField; }
            set { this.satField = value; }
        }
        public decimal hdop {
            get { return this.hdopField; }
            set { this.hdopField = value; }
        }
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool hdopSpecified {
            get { return this.hdopFieldSpecified; }
            set { this.hdopFieldSpecified = value; }
        }
        public decimal vdop {
            get { return this.vdopField; }
            set { this.vdopField = value; }
        }
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool vdopSpecified {
            get { return this.vdopFieldSpecified; }
            set { this.vdopFieldSpecified = value; }
        }
        public decimal pdop {
            get { return this.pdopField; }
            set { this.pdopField = value;
            }
        }
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool pdopSpecified {
            get { return this.pdopFieldSpecified; }
            set { this.pdopFieldSpecified = value; }
        }
        public decimal ageofdgpsdata {
            get { return this.ageofdgpsdataField; }
            set { this.ageofdgpsdataField = value;
            }
        }
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool ageofdgpsdataSpecified {
            get {return this.ageofdgpsdataFieldSpecified; }
            set { this.ageofdgpsdataFieldSpecified = value; }
        }
        [System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
        public string dgpsid {
            get { return this.dgpsidField; }
            set { this.dgpsidField = value; }
        }
        public extensionsType extensions {
            get { return this.extensionsField; }
            set { this.extensionsField = value; }
        }
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal lat {
            get { return this.latField; }
            set { this.latField = value; }
        }
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal lon {
            get { return this.lonField; }
            set { this.lonField = value; }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    public partial class linkType {
        private string textField;
        private string typeField;
        private string hrefField;
        public string text {
            get {
                return this.textField;
            }
            set {
                this.textField = value;
            }
        }
        public string type {
            get {
                return this.typeField;
            }
            set {
                this.typeField = value;
            }
        }
        [System.Xml.Serialization.XmlAttributeAttribute(DataType="anyURI")]
        public string href {
            get {
                return this.hrefField;
            }
            set {
                this.hrefField = value;
            }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    public enum fixType {
        none,
        [System.Xml.Serialization.XmlEnumAttribute("2d")]
        Item2d,
        [System.Xml.Serialization.XmlEnumAttribute("3d")]
        Item3d,
        dgps,
        pps,
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    public partial class extensionsType {
        private System.Xml.XmlElement[] anyField;
        [System.Xml.Serialization.XmlAnyElementAttribute()]
        public System.Xml.XmlElement[] Any {
            get {
                return this.anyField;
            }
            set {
                this.anyField = value;
            }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    public partial class trkType {
        private string nameField;
        private string cmtField;
        private string descField;
        private string srcField;
        private linkType[] linkField;
        private string numberField;
        private string typeField;
        private extensionsType extensionsField;
        private trksegType[] trksegField;
        public string name {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }
        public string cmt {
            get {
                return this.cmtField;
            }
            set {
                this.cmtField = value;
            }
        }
        public string desc {
            get {
                return this.descField;
            }
            set {
                this.descField = value;
            }
        }
        public string src {
            get {
                return this.srcField;
            }
            set {
                this.srcField = value;
            }
        }
        [System.Xml.Serialization.XmlElementAttribute("link")]
        public linkType[] link {
            get {
                return this.linkField;
            }
            set {
                this.linkField = value;
            }
        }
        [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")]
        public string number {
            get {
                return this.numberField;
            }
            set {
                this.numberField = value;
            }
        }
        public string type {
            get {
                return this.typeField;
            }
            set {
                this.typeField = value;
            }
        }
        public extensionsType extensions {
            get {
                return this.extensionsField;
            }
            set {
                this.extensionsField = value;
            }
        }
        [System.Xml.Serialization.XmlElementAttribute("trkseg")]
        public trksegType[] trkseg {
            get {
                return this.trksegField;
            }
            set {
                this.trksegField = value;
            }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    public partial class rteType {
        private string nameField;
        private string cmtField;
        private string descField;
        private string srcField;
        private linkType[] linkField;
        private string numberField;
        private string typeField;
        private extensionsType extensionsField;
        private wptType[] rteptField;
        public string name {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }
        public string cmt {
            get {
                return this.cmtField;
            }
            set {
                this.cmtField = value;
            }
        }
        public string desc {
            get {
                return this.descField;
            }
            set {
                this.descField = value;
            }
        }
        public string src {
            get {
                return this.srcField;
            }
            set {
                this.srcField = value;
            }
        }
        [System.Xml.Serialization.XmlElementAttribute("link")]
        public linkType[] link {
            get {
                return this.linkField;
            }
            set {
                this.linkField = value;
            }
        }
        [System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")]
        public string number {
            get {
                return this.numberField;
            }
            set {
                this.numberField = value;
            }
        }
        public string type {
            get {
                return this.typeField;
            }
            set {
                this.typeField = value;
            }
        }
        public extensionsType extensions {
            get {
                return this.extensionsField;
            }
            set {
                this.extensionsField = value;
            }
        }
        [System.Xml.Serialization.XmlElementAttribute("rtept")]
        public wptType[] rtept {
            get {
                return this.rteptField;
            }
            set {
                this.rteptField = value;
            }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.topografix.com/GPX/1/1")]
    public partial class boundsType
    {
        private decimal minlatField;
        private decimal minlonField;
        private decimal maxlatField;
        private decimal maxlonField;
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal minlat
        {
            get
            {
                return this.minlatField;
            }
            set
            {
                this.minlatField = value;
            }
        }
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal minlon
        {
            get
            {
                return this.minlonField;
            }
            set
            {
                this.minlonField = value;
            }
        }
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal maxlat
        {
            get
            {
                return this.maxlatField;
            }
            set
            {
                this.maxlatField = value;
            }
        }
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal maxlon
        {
            get
            {
                return this.maxlonField;
            }
            set
            {
                this.maxlonField = value;
            }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.topografix.com/GPX/1/1")]
    public partial class copyrightType {
        private string yearField;
        private string licenseField;
        private string authorField;
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(DataType="gYear")]
        public string year {
            get {
                return this.yearField;
            }
            set {
                this.yearField = value;
            }
        }
        [System.Xml.Serialization.XmlElementAttribute(DataType="anyURI")]
        public string license {
            get {
                return this.licenseField;
            }
            set {
                this.licenseField = value;
            }
        }
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string author {
            get {
                return this.authorField;
            }
            set {
                this.authorField = value;
            }
        }
    }
}

#3楼 票数:0

我不是为什么,但是下面的代码对我来说很好。 我仍然没有为他们找到正确的xpath。

即使我获得了“ trkpt”节点,我仍然无法获得具有xpath的子节点,而不是完美的解决方案。

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(sss);

    XmlNodeList root = doc.GetElementsByTagName("trkpt");
    Console.WriteLine(root.Count);

    for (int i = 0; i < root.Count; i++)
    {
        Console.WriteLine("attr:" +root[i].Attributes["lat"].Value);
        Console.WriteLine("attr:" +root[i].Attributes["lon"].Value);
        Console.WriteLine("subnode:" +root[i].SelectNodes("./*")[0].InnerText);
        Console.WriteLine("subnode:" +root[i].SelectNodes("./*")[1].InnerText);
        Console.WriteLine("----");
    }

#4楼 票数:0 已采纳

如果您只需要纬度和经度,则可以使用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\gps.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement root = doc.Root;
            XNamespace ns = root.GetDefaultNamespace();

            var results = doc.Descendants(ns + "trkpt").Select(x => new
            {
                lat = x.Attribute("lat"),
                lon = x.Attribute("lon"),
                ele = (decimal)x.Element(ns + "ele"),
                time = (DateTime)x.Element(ns + "time")
            }).ToList();

        }
    }
}

  ask by ccumaya translate from so

未解决问题?本站智能推荐:

1回复

解析特定元素值的xml

我有像xml一样的 同样,我有很多列车编号及其详细信息。 我想解析特定列车编号的xml并获取与之相关的所有详细信息。 就像我想获得2号火车的详细信息一样。 我使用xpath作为: 它不起作用,是什么方法? 这可以通过Linq Xml完成吗? 怎么样?
2回复

使用XmlNode进行XML解析

在我的应用程序中,我得到一个XML Response并且试图使用XmlNode转换该XML响应。 我的XML文件: 文件 XML内容将类似于: 在这里,我展示了该XML响应中的两个示例帐户,我需要遍历所有account nodes并获得zimbraMailDeliveryAd
1回复

使用nav.SelectSingleNode时是否有任何欺骗名称空间的方法

我正在尝试创建一个应用程序,该应用程序将输出某些xpath中存在的数据(将由用户指定) 这里的变量文件是xml文件的位置。 现在,当我在上面定义了很多命名空间的xml文件上运行此代码时,上面的代码将返回nullreference异常,因为变量结果为null,并且我正在尝试访问 re
3回复

当XML再次包含相同元素时,如何使用C#解析XML?

我正在尝试读取看起来像的XML文件, 我试图读取读取的第三个值。 我的代码能够检索第一个值。 为了读取第三个或第四个值,我需要进行哪些更改? 解决方案:(由@DGibbs提供)
1回复

解析内部XML分支

我有一个XML文件,看起来像: 我使用以下代码解析相关分支以更新目录/文件信息: 如果XML文件中只有目录,代码就像我期望的那样工作,但是如果我有多个条目,则会在尝试多次解析文件节点并且XML文件永远不会更新时抛出错误。 如果我删除了代码的更新文件信息部分,则目录分支正确更新。
2回复

无法使用XPath读取xml

我需要在XMind包中解析content.xml文件。 文件看起来像这样 我需要获得所有表格的标题列表。 我正在使用XPathExpression expr = navigator.Compile("//sheet"); 但我想它不起作用 该怎么办。 任何建议或代码都
1回复

xml解析错误(xpath,HTMLagilitypack)

我正在尝试解析xml。 除一个节点在某些行中仅具有以下标记外,所有节点都具有开始和结束标记: <persons/> 在大多数情况下,它看起来是这样的: <persons> ... </persons>当此节点未关闭时,我无法从xml中获取值
4回复

XML解析和转换(XSLT或其他方式)

我有几个以这种方式格式化的xml文件: 我该如何格式化?: 我知道一些C#,也许在那里有解析器? 还是一些通用的xslt? xml文件是从客户端接收的一些数据,因此我无法控制它们向我发送数据的方式。 LE基本上,当我尝试在excel中测试此数据时(例如,我想确保chil