前提
KMLファイルを読み込み、白地図を作成したいです。
実現したいこと
KMLファイルの読み込み
発生している問題
XMLファイルと同様の操作をしても、うまく読み込めない。
(XMLファイルでは動作確認済み)
該当のソースコード
C#
using System;using System.Collections;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Xml;using System.Xml.Linq;using System.IO;using System.Runtime.InteropServices; namespace mapquest_homework { public partial class Form1 : Form { [DllImport("kernel32.dll")] private static extern bool AllocConsole(); [DllImport("kernel32.dll")] private static extern bool FreeConsole(); /// <summary> /// Windows FormアプリケーションでConsoleを利用する最低限のサンプル /// </summary> private string[][] DataArray; private ArrayList debug = new ArrayList(); public Form1() { InitializeComponent(); AllocConsole(); } private void DataLoadButton_Click(object sender, EventArgs e) { var xmlDoc = new XmlDocument(); xmlDoc.Load(@"C:/Users/niino/Desktop/kmlファイル/kmlファイル/ITA_adm0.kml"); var Placemark = xmlDoc.SelectNodes("kml/Document"); foreach (XmlNode pl in Placemark) { debug.Add("aaa"); //座標データの原本を格納 var data = pl.SelectSingleNode("name").InnerText; debug.Add(data); } } private void ShowConsoleButton_Click(object sender, EventArgs e) { Console.WriteLine("debugの要素数は:" + debug.Count); for(int i = 0; i < debug.Count; i++) { Console.WriteLine(debug[i]); } } }}
KMLfile
<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.2"> <Document> <name>Italy</name> <description><![CDATA[<a href='http://www.gadm.org'>GADM</a> boundaries.]]></description> <Style id="1"> <LineStyle> <width>1.5</width> <color>ff000000</color> </LineStyle> <PolyStyle> <color>aa0000df</color> <fill>1</fill> </PolyStyle> </Style> <Placemark> <name>Italy</name> //以下略
試したこと
XMLファイルではできていたので、KMLとXMLの違う点が問題だと考え、KMLファイルの二行目の xmins の部分を削り、<kmll></kml>のみにしたらうまく読み込めました。ただKMLファイル自体を加工したくないので、何とかC#のコードを改変して実現したいです。
補足情報(FW/ツールのバージョンなど)
VisualStudio2019
C# .NET Framework 4.7.2
windows form アプリケーション
0 コメント