提问人:Olio 提问时间:6/10/2021 更新时间:6/10/2021 访问量:79
使用 XElement 访问 XML 数据不知何故没有显示值 - Schema?[复制]
XML data access with XElement somehow does not show the value - Schema? [duplicate]
问:
请帮助我提供任何建议,因为我还远远不知道 XDocument XElement 如何与 Schemas 一起工作......
我实际上只想解析内容,而不验证 XML - 只想读取数据:
下面是 XML:
<?xml version="1.0" encoding="utf-8"?><gpx creator="Garmin Desktop App" 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/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd
http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd
http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd
http://www.garmin.com/xmlschemas/ActivityExtension/v1 http://www8.garmin.com/xmlschemas/ActivityExtensionv1.xsd
http://www.garmin.com/xmlschemas/AdventuresExtensions/v1 http://www8.garmin.com/xmlschemas/AdventuresExtensionv1.xsd
http://www.garmin.com/xmlschemas/PressureExtension/v1 http://www.garmin.com/xmlschemas/PressureExtensionv1.xsd
http://www.garmin.com/xmlschemas/TripExtensions/v1 http://www.garmin.com/xmlschemas/TripExtensionsv1.xsd
http://www.garmin.com/xmlschemas/TripMetaDataExtensions/v1 http://www.garmin.com/xmlschemas/TripMetaDataExtensionsv1.xsd
http://www.garmin.com/xmlschemas/ViaPointTransportationModeExtensions/v1 http://www.garmin.com/xmlschemas/ViaPointTransportationModeExtensionsv1.xsd
http://www.garmin.com/xmlschemas/CreationTimeExtension/v1 http://www.garmin.com/xmlschemas/CreationTimeExtensionsv1.xsd
http://www.garmin.com/xmlschemas/AccelerationExtension/v1 http://www.garmin.com/xmlschemas/AccelerationExtensionv1.xsd
http://www.garmin.com/xmlschemas/PowerExtension/v1 http://www.garmin.com/xmlschemas/PowerExtensionv1.xsd
http://www.garmin.com/xmlschemas/VideoExtension/v1 http://www.garmin.com/xmlschemas/VideoExtensionv1.xsd"
xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wptx1="http://www.garmin.com/xmlschemas/WaypointExtension/v1" xmlns:gpxtrx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
xmlns:trp="http://www.garmin.com/xmlschemas/TripExtensions/v1" xmlns:adv="http://www.garmin.com/xmlschemas/AdventuresExtensions/v1"
xmlns:prs="http://www.garmin.com/xmlschemas/PressureExtension/v1" xmlns:tmd="http://www.garmin.com/xmlschemas/TripMetaDataExtensions/v1"
xmlns:vptm="http://www.garmin.com/xmlschemas/ViaPointTransportationModeExtensions/v1" xmlns:ctx="http://www.garmin.com/xmlschemas/CreationTimeExtension/v1"
xmlns:gpxacc="http://www.garmin.com/xmlschemas/AccelerationExtension/v1" xmlns:gpxpx="http://www.garmin.com/xmlschemas/PowerExtension/v1"
xmlns:vidx1="http://www.garmin.com/xmlschemas/VideoExtension/v1">
<metadata>
<link href="http://www.garmin.com">
<text>Garmin International</text>
</link>
<time>2018-03-14T18:43:36Z</time>
<bounds maxlat="45.255227560139" maxlon="28.8452932373" minlat="43.22223" minlon="14.26041412592" />
</metadata>
<trk>
<name>TEST</name>
<extensions>
<gpxx:TrackExtension>
<gpxx:DisplayColor>Red</gpxx:DisplayColor>
</gpxx:TrackExtension>
</extensions>
<trkseg>
<trkpt lat="44.177107578143477" lon="16.26372094">
<ele>0</ele>
<time>2016-12-27T04:59:20Z</time>
</trkpt>
<trkpt lat="44.177290974184871" lon="16.2639852379">
<ele>0</ele>
<time>2016-12-28T04:59:40Z</time>
</trkpt>
<trkpt lat="44.178708018735051" lon="16.261576104">
<ele>0</ele>
<time>2016-12-29T05:00:00Z</time>
</trkpt>
<trkpt lat="44.179137004539371" lon="16.26883971">
<ele>0</ele>
<time>2016-12-30T05:00:20Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>
但是:我可以加载 XElement,但 foreach 没有显示任何内容。我做错了什么?
var xelem = XElement.Load(whateverxml);
foreach (XElement ele in xelem.Descendants("time"))
Debug.Log(ele.Value);
答:
0赞
Ming Shou
6/10/2021
#1
而不是
foreach (XElement ele in xelem.Descendants("time"))
请使用以下代码。
XName xName = XName.Get("time", "http://www.topografix.com/GPX/1/1");
foreach (XElement ele in xelem.Descendants(xName))
或
foreach (XElement ele in xelem.Descendants().Where(a => a.Name.LocalName == "time"))
然后,它会起作用。
评论
0赞
Olio
6/10/2021
作品;)这是怎么回事?所以你在 LINQ 上做了一个 lambda ...有趣。。。感谢
0赞
Ming Shou
6/10/2021
问题的原因是命名空间。如果它对您有帮助,请投票。
0赞
Olio
6/10/2021
会这样做的,当我达到 15 个声誉时......同时我不能......感谢
评论
XXXX:
xmlns="http://www.topografix.com/GPX/1/1"