如何使用XmlDocument使用名称空间解析XML

时间:2018-10-12 15:29:58

标签: c# asp.net xml xpath xml-parsing

我运行这段代码 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>

感谢您的帮助

4 个答案:

答案 0 :(得分: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/en-us/download/details.aspx?id=19988)。我从cmd.exe> xsd.exe / c / l:cs gps.xsd使用了以下命令行

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

答案 1 :(得分: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;
            }
        }
    }
}

答案 2 :(得分: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("----");
    }

答案 3 :(得分:0)

如果您只需要纬度和经度,则可以使用xml linq和以下代码轻松获取它:

y = string(myCellArray{2:14675, 1})