【问题标题】:Can you help me parse this kml in c#? [closed]你能帮我用c#解析这个kml吗? [关闭]
【发布时间】:2012-09-09 13:01:57
【问题描述】:

我从位置 weeb 服务获得了这个 KML,我需要对其进行解析。我需要读取所有的 PLACEMARK 标签,我只需要它们的名称纬度和经度。感谢您的帮助。

<?xml version="1.0" encoding="UTF-8" ?> 
 <kml xmlns="http://www.opengis.net/kml/2.2">
 <Document>
  <name>Lociraj.net (C) 2010</name> 
 <StyleMap id="highlighting">
 <Pair>
  <key>normal</key> 
  <styleUrl>#normalState</styleUrl> 
  </Pair>
 <Pair>
  <key>highlight</key> 
  <styleUrl>#highlightedState</styleUrl> 
  </Pair>
  </StyleMap>
 <Style id="highlightedState">
 <IconStyle>
  <scale>1.1</scale> 
 <Icon>
  <href>http://maps.google.com/mapfiles/kml/pal3/icon55.png</href> 
  </Icon>
  </IconStyle>
 <LabelStyle>
  <scale>1.1</scale> 
  </LabelStyle>
  </Style>
 <Style id="normalState">
 <IconStyle>
 <Icon>
  <href>http://maps.google.com/mapfiles/kml/pal3/icon63.png</href> 
  </Icon>
  </IconStyle>
  </Style>
 <Placemark>
  <name>Vaša lokacija</name> 
  <description /> 
 <LookAt>
  <longitude>15.976903</longitude> 
  <latitude>45.813182</latitude> 
  <altitude>0</altitude> 
  <range>500</range> 
  </LookAt>
 <Style>
 <IconStyle>
 <Icon>
  <href>http://lociraj.net/img/icon_map_your_location.png</href> 
  </Icon>
  </IconStyle>
  </Style>
 <Point>
  <coordinates>15.976903,45.813182,0</coordinates> 
  </Point>
  </Placemark>
 <Folder>
  <name>Bankomati</name> 
 <Placemark>
  <name>Raiffeisen</name> 
 <description>
 <![CDATA[ Ilica 1a, Zagreb<br><br>Raiffeisenbank<br>
<b>Udaljenost:</b> 65m<br>

  ]]> 
  </description>
 <LookAt>
  <longitude>15.9761</longitude> 
  <latitude>45.813</latitude> 
  <altitude>0</altitude> 
  <range>500</range> 
  </LookAt>
 <Style>
 <IconStyle>
 <Icon>
  <href>http://lociraj.net/img/icon_map_atm.png</href> 
  </Icon>
  </IconStyle>
  </Style>
 <ExtendedData xmlns:l="http://lociraj.net">
  <l:categoryID>1</l:categoryID> 
  <l:entryID>1782</l:entryID> 
  </ExtendedData>
 <Point>
  <coordinates>15.9761,45.813,0</coordinates> 
  </Point>
  </Placemark>
 <Placemark>
  <name>PBZ</name> 
 <description>
 <![CDATA[ Trg bana J. Jelačića 7, Zagreb<br><br><br>
<b>Udaljenost:</b> 66m<br>

  ]]> 
  </description>
 <LookAt>
  <longitude>15.9777218535</longitude> 
  <latitude>45.8133623448</latitude> 
  <altitude>0</altitude> 
  <range>500</range> 
  </LookAt>
 <Style>
 <IconStyle>
 <Icon>
  <href>http://lociraj.net/img/icon_map_atm.png</href> 
  </Icon>
  </IconStyle>
  </Style>
 <ExtendedData xmlns:l="http://lociraj.net">
  <l:categoryID>1</l:categoryID> 
  <l:entryID>921</l:entryID> 
  </ExtendedData>
 <Point>
  <coordinates>15.9777218535,45.8133623448,0</coordinates> 
  </Point>
  </Placemark>
 <Placemark>
  <name>Erste</name> 
 <description>
 <![CDATA[ Ilica bb, Zagreb<br><br>ZET okretište - Črnomerec<br>
<b>Udaljenost:</b> 68m<br>

  ]]> 
  </description>
 <LookAt>
 <longitude>15.976051</longitude> 
  <latitude>45.81303</latitude> 
  <altitude>0</altitude> 
  <range>500</range> 
  </LookAt>
 <Style>
 <IconStyle>
 <Icon>
  <href>http://lociraj.net/img/icon_map_atm.png</href> 
  </Icon>
  </IconStyle>
  </Style>
 <ExtendedData xmlns:l="http://lociraj.net">
  <l:categoryID>1</l:categoryID> 
  <l:entryID>309</l:entryID> 
  </ExtendedData>
 <Point>
  <coordinates>15.976051,45.81303,0</coordinates> 
  </Point>
  </Placemark>
 <Placemark>
  <name>Erste</name> 
 <description>
 <![CDATA[ Ilica 1, Zagreb<br><br>Prolaz - Neboder<br>
<b>Udaljenost:</b> 68m<br>

  ]]> 
  </description>
 <LookAt>
  <longitude>15.976051</longitude> 
  <latitude>45.81303</latitude> 
  <altitude>0</altitude> 
  <range>500</range> 
  </LookAt>
 <Style>
<IconStyle>
 <Icon>
  <href>http://lociraj.net/img/icon_map_atm.png</href> 
  </Icon>
  </IconStyle>
  </Style>
 <ExtendedData xmlns:l="http://lociraj.net">
  <l:categoryID>1</l:categoryID> 
  <l:entryID>300</l:entryID> 
  </ExtendedData>
 <Point>
  <coordinates>15.976051,45.81303,0</coordinates> 
  </Point>
  </Placemark>
 <Placemark>
  <name>ZaBa</name> 
 <description>
<![CDATA[ Ilica bb, Zagreb<br><br>ZET okretište - Črnomerec<br>
<b>Udaljenost:</b> 68m<br>

  ]]> 
  </description>
 <LookAt>
  <longitude>15.976051</longitude> 
  <latitude>45.81303</latitude> 
  <altitude>0</altitude> 
  <range>500</range> 
  </LookAt>
 <Style>
 <IconStyle>
 <Icon>
  <href>http://lociraj.net/img/icon_map_atm.png</href> 
  </Icon>
  </IconStyle>
  </Style>
 <ExtendedData xmlns:l="http://lociraj.net">
  <l:categoryID>1</l:categoryID> 
  <l:entryID>1519</l:entryID> 
  </ExtendedData>
 <Point>
  <coordinates>15.976051,45.81303,0</coordinates> 
  </Point>
  </Placemark>
  </Folder>
  </Document>
  <kml>

你能帮我分析一下吗

【问题讨论】:

  • 使用LINQ to XML 或类似的东西,然后自己尝试一下。如果遇到麻烦,请随时提出有关 SO 的具体问题。
  • Stack 不是“为我做”类型的交易。自己尝试一下,如果卡住了,请寻​​求帮助。 -1 表示缺乏尝试。
  • Ofc 它不是我尝试了 10 个小时,然后问。尝试解析你会发现有问题可能不是有效的 xml 或其他东西
  • @DživoJelić 到底是什么问题?你试过什么?错误的结果是什么?
  • 与答案相同,但未使用 xnamespace,结果为空

标签: c# xml kml


【解决方案1】:
XDocument xDoc = XDocument.Load(....);
XNamespace ns = "http://www.opengis.net/kml/2.2";

var placemarks = xDoc
            .Descendants(ns + "Placemark")
            .Select(p => new
            {
                Name = p.Element(ns+"name").Value,
                Longitude = p.Element(ns+"LookAt").Element(ns+"longitude").Value,
                Latitude = p.Element(ns+"LookAt").Element(ns+"latitude").Value,
            })
            .ToArray();

【讨论】:

  • thx 它解决了我对 linq 的新问题。非常感谢
猜你喜欢
  • 2021-01-28
  • 2017-12-23
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多