如何选择XDocument中的项目?

时间:2016-04-26 17:29:52

标签: c# xml linq-to-xml

在下面的XML中,我尝试检索所有trkpt项,以便我可以使用lat / lon属性值执行任务。我认为LINQ to XML是最简单的方法,但我无法在foreach循环中返回任何结果。我做错了什么?

此代码位于控制台应用程序的Main()方法中:

var filename = @"C:\temp\sample_output.gpx";

var xDoc = XDocument.Load(filename);

XNamespace xmlns = "http://www.topografix.com/GPX/1/1";
XNamespace gpxtpx = "http://www.garmin.com/xmlschemas/TrackPointExtension/v1";
XNamespace wptx1 = "http://www.garmin.com/xmlschemas/WaypointExtension/v1";
XNamespace gpxx = "http://www.garmin.com/xmlschemas/GpxExtensions/v3";
XNamespace gpxtrkx = "http://www.garmin.com/xmlschemas/TrackStatsExtension/v1";

foreach (var item in xDoc.Descendants(gpxtpx + "trkpt"))
{
    Console.WriteLine(item.Element(gpxtpx + "trkpt").Value);
}

Console.Read();

XML数据我尝试使用上面的代码进行处理。 Lat / Lon值已修改。

 <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    <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"
creator="Oregon 600" version="1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>2016-04-18T01:19:07Z</time>
        </metadata>
        <trk>
            <name>2016-04-17 20:19:01</name>
            <extensions>
                <gpxx:TrackExtension>
                    <gpxx:DisplayColor>Magenta</gpxx:DisplayColor>
                </gpxx:TrackExtension>
            </extensions>
            <trkseg>
                <trkpt lat="44.123" lon="-89.123">
                    <ele>343.61</ele>
                    <time>2016-04-17T22:53:34Z</time>
                </trkpt>
                <trkpt lat="44.123" lon="-89.123">
                    <ele>343.58</ele>
                    <time>2016-04-17T22:53:35Z</time>
                </trkpt>
                <trkpt lat="44.123" lon="-89.123">
                    <ele>343.56</ele>
                    <time>2016-04-17T22:53:36Z</time>
                </trkpt>
                <trkpt lat="44.123" lon="-89.123">
                    <ele>343.55</ele>
                    <time>2016-04-17T22:53:37Z</time>
                </trkpt>
                <trkpt lat="44.123" lon="-89.123">
                    <ele>343.62</ele>
                    <time>2016-04-17T22:53:38Z</time>
                </trkpt>
            </trkseg>
        </trk>
    </gpx>

2 个答案:

答案 0 :(得分:4)

您正在使用错误的命名空间。元素的默认命名空间(包括http://www.topografix.com/GPX/1/1)为xmlns="http://www.topografix.com/GPX/1/1" ,原因如下:

gpxNs

我强烈建议您将变量名称更改为XNamespace gpxNs = "http://www.topografix.com/GPX/1/1"; ... foreach (var item in xDoc.Descendants(gpxNs + "trkpt")) 以指示其值,然后使用它:

PropertiesService

答案 1 :(得分: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\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement trkseg = doc.Descendants().Where(x => x.Name.LocalName == "trkseg").FirstOrDefault();
            XNamespace ns = trkseg.Name.Namespace;

            var l_l = trkseg.Elements(ns + "trkpt").Select(x => new
            {
                lat = x.Attribute("lat").Value,
                lon = x.Attribute("lon").Value
            }).ToList();

        }
    }
}