Reading in XML/KML files using C#

后端 未结 5 1370
遥遥无期
遥遥无期 2021-02-03 11:28

I\'m trying to import the kml xml Google earth file into an application, but i can\'t seem to get the xDocument syntax right in order to do what i want, i\'m wondering if anyone

5条回答
  •  伪装坚强ぢ
    2021-02-03 12:04

    I used SharmpKml and its documentation to extract information from KML file.

    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())
      {
      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())
      {
      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;
        }
      }
    }
    

提交回复
热议问题