Linq to XML を使用して XML テキストを解析しています。返された XDocument は、このすべての名前空間を各ノードに追加し、子孫を見つけることを不可能にするか、少なくとも "Placemark" の検索が機能しません。おそらく、kml:Placemark が検索に適合しないためです。
これを単体テストで基本的な XML ファイルを使用してテストしたところ、問題なく動作しました。私は、XML 宣言部分にすべての名前空間が含まれていなかったと推測しています。
すべての名前空間を追加せずに XML テキストを解析するにはどうすればよいですか?
解析:
XDocument document = XDocument.Parse(text);
var polygonNodes = document.Descendants("Polygon"); // yields no nodes, even though there are descendants with this.
元のファイル
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>poly1_philmont.kml</name>
<Style id="s_ylw-pushpin_hl">
<IconStyle>
<scale>1.3</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
<LineStyle>
<color>ff0000aa</color>
</LineStyle>
<PolyStyle>
<color>33ffaa00</color>
</PolyStyle>
</Style>
... </kml>
XDocument で解析 (ルート ノード)
<kml:kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<kml:Document>
<kml:name>poly1_philmont.kml</kml:name>
<kml:Style id="s_ylw-pushpin_hl">
<kml:IconStyle>
<kml:scale>1.3</kml:scale>
<kml:Icon>
<kml:href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</kml:href>
</kml:Icon>
<kml:hotSpot x="20" y="2" xunits="pixels" yunits="pixels" />
</kml:IconStyle>
<kml:LineStyle>
<kml:color>ff0000aa</kml:color>
</kml:LineStyle>
<kml:PolyStyle>
<kml:color>33ffaa00</kml:color>
</kml:PolyStyle>
</kml:Style>
...
</kml:kml>