使用C#读取XML / KML文件

时间:2012-10-08 19:29:34

标签: c# xml entity-framework linq kml

我正在尝试将kml xml Google地球文件导入到应用程序中,但我似乎无法正确获取xDocument语法以执行我想要的操作,我想知道是否有人可以建议的方法读入kml xml文件。

我理解xml导入的基础知识但是无法使用xDocument和Linq,理想情况下我想将每个Placemark作为对象并将它们添加到我的Entity Framework驱动的db中。关于我应该怎么做的任何建议都会很棒,因为我刚刚开始使用Linq并且可以做一些指针。 xml的布局如下

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
  <Document>
    <Placemark>
      <name>XXX</name>
      <description>XXX</description>
      <styleUrl>XXX</styleUrl>
      <Point>
         <coordinates>XXX</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <name>XXX</name>
      <description>XXX</description>
      <styleUrl>XXX</styleUrl>
      <Point>
         <coordinates>XXX</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>

5 个答案:

答案 0 :(得分:8)

您没有包含任何代码,但我猜您在引用内容时忘记包含命名空间。这是一个例子。

基本访问:

var placemarks = xdoc.Element("kml").Element("Document").Elements("Placemark");

使用名称空间:

var ns = XNamespace.Get("http://earth.google.com/kml/2.2");
var placemarks = xdoc.Element(ns + "kml").Element(ns + "Document").Elements(ns + "Placemark");

答案 1 :(得分:6)

我的猜测是您忘记在LINQ to XML查询中使用命名空间。从这里提取数据很容易:

XNamespace ns = "http://earth.google.com/kml/2.2";
var doc = XDocument.Load("file.xml");
var query = doc.Root
               .Element(ns + "Document")
               .Elements(ns + "Placemark")
               .Select(x => new PlaceMark // I assume you've already got this
                       {
                           Name = x.Element(ns + "name").Value,
                           Description = x.Element(ns + "description").Value,
                           // etc
                       });

如果这没有帮助,请发布您尝试过的完整示例以及出现的问题。

答案 2 :(得分:3)

var xDoc = XDocument.Load("a.xml");
XNamespace ns = "http://earth.google.com/kml/2.2";

var placemarks = xDoc.Descendants(ns+"Placemark")
                    .Select(p => new
                    {
                        Name = p.Element(ns+"name").Value,
                        Desc = p.Element(ns+"description").Value
                    })
                    .ToList();

答案 3 :(得分:3)

我使用SharmpKml及其documentation从KML文件中提取信息。

using SharpKml.Dom;
using SharpKml.Engine;
using SharpKml.Dom.GX;

TextReader reader = File.OpenText(filePath);
KmlFile file = KmlFile.Load(reader);
_kml = file.Root as Kml;

sPlaceMarks[] tempPlaceMarks = new sPlaceMarks[1000];
if (_kml != null)
{
  foreach (var placemark in _kml.Flatten().OfType<Placemark>())
  {
  tempPlaceMarks[numOfPlaceMarks].Name = placemark.Name;
  tempPlaceMarks[numOfPlaceMarks].Description = placemark.Description.Text;
  tempPlaceMarks[numOfPlaceMarks].StyleUrl = placemark.StyleUrl;
  tempPlaceMarks[numOfPlaceMarks].point = placemark.Geometry as SharpKml.Dom.Point;
  tempPlaceMarks[numOfPlaceMarks].CoordinateX = tempPlaceMarks[numOfPlaceMarks].point.Coordinate.Longitude;
  tempPlaceMarks[numOfPlaceMarks].CoordinateY = tempPlaceMarks[numOfPlaceMarks].point.Coordinate.Latitude;
  tempPlaceMarks[numOfPlaceMarks].CoordinateZ = tempPlaceMarks[numOfPlaceMarks].point.Coordinate.Altitude;
  numOfPlaceMarks++;
  }

  foreach (var lookAt in _kml.Flatten().OfType<LookAt>())
  {
  Placemark placemark = (Placemark)lookAt.Parent;
  for (int i = 0; i < numOfPlaceMarks; i++)
  {
    if (placemark.Name == tempPlaceMarks[i].Name)
    {
      tempPlaceMarks[i].Name = placemark.Name;
      tempPlaceMarks[i].Description = placemark.Description.Text;
      tempPlaceMarks[i].StyleUrl = placemark.StyleUrl;
      tempPlaceMarks[i].altitude = lookAt.Altitude;
      tempPlaceMarks[i].AltitudeMode =(SharpKml.Dom.GX.AltitudeMode)lookAt.GXAltitudeMode;
      tempPlaceMarks[i].Heading = lookAt.Heading;
      tempPlaceMarks[i].Latitude = lookAt.Latitude;
      tempPlaceMarks[i].Longitude = lookAt.Longitude;
      tempPlaceMarks[i].Range = lookAt.Range;
      tempPlaceMarks[i].Tilt = lookAt.Tilt;
      break;
    }
  }
}

答案 4 :(得分:0)

您的kml文件必须包含

<kml xmlns="http://www.opengis.net/kml/2.2" ...

代替

<kml xmlns="http://earth.google.com/kml/2.2"> ...