使用C#读取XML/KML文件

nor*_*ert 10 c# xml linq entity-framework 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>
Run Code Online (Sandbox Code Playgroud)

Guv*_*nte 8

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

基本访问:

var placemarks = xdoc.Element("kml").Element("Document").Elements("Placemark");
Run Code Online (Sandbox Code Playgroud)

使用命名空间:

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


Jon*_*eet 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
                       });
Run Code Online (Sandbox Code Playgroud)

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


Has*_*man 5

我使用SharmpKml及其文档从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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)