2016-09-14 9 views
0
<?xml version="1.0" encoding="UTF-8"?> 

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

    xmlns:gx="http://www.google.com/kml/ext/2.2" 

xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"> 

<Document> 
<name>Untitled Folder.kmz</name> 
<Style id="s_ylw-pushpin0"> 
    <IconStyle> 
     <scale>1.1</scale> 
     <Icon> 
      <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href> 
     </Icon> 
     <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/> 
    </IconStyle> 
</Style> 
<Style id="s_ylw-pushpin_hl00"> 
    <IconStyle> 
     <scale>1.3</scale> 
     <Icon> 
      <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href> 
     </Icon> 
     <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/> 
    </IconStyle> 
</Style> 
<Style id="s_ylw-pushpin_hl0"> 
    <IconStyle> 
     <scale>1.3</scale> 
     <Icon> 
      <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href> 
     </Icon> 
     <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/> 
    </IconStyle> 
</Style> 
<StyleMap id="m_ylw-pushpin0"> 
    <Pair> 
     <key>normal</key> 
     <styleUrl>#s_ylw-pushpin0</styleUrl> 
    </Pair> 
    <Pair> 
     <key>highlight</key> 
     <styleUrl>#s_ylw-pushpin_hl00</styleUrl> 
    </Pair> 
</StyleMap> 
<StyleMap id="m_ylw-pushpin"> 
    <Pair> 
     <key>normal</key> 
     <styleUrl>#s_ylw-pushpin</styleUrl> 
    </Pair> 
    <Pair> 
     <key>highlight</key> 
     <styleUrl>#s_ylw-pushpin_hl0</styleUrl> 
    </Pair> 
</StyleMap> 
<Style id="s_ylw-pushpin"> 
    <IconStyle> 
     <scale>1.1</scale> 
     <Icon> 
      <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href> 
     </Icon> 
     <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/> 
    </IconStyle> 
</Style> 
<Folder> 
    <name>Untitled Folder</name> 
    <open>1</open> 
    <Placemark> 
     <name>4</name> 
     <styleUrl>#m_ylw-pushpin</styleUrl> 
     <LineString> 
      <tessellate>1</tessellate> 
      <coordinates> 
       80.39720270023678,14.81309175627283,0 80.42184703577358,15.30217528568833,0 
      </coordinates> 
     </LineString> 
    </Placemark> 
    <Placemark> 
     <name>3</name> 
     <styleUrl>#m_ylw-pushpin</styleUrl> 
     <LineString> 
      <tessellate>1</tessellate> 
      <coordinates> 
       80.99282826200491,14.7585958192514,0 80.39353985420162,14.80969021928927,0 
      </coordinates> 
     </LineString> 
    </Placemark> 
    <Placemark> 
     <name>2</name> 
     <styleUrl>#m_ylw-pushpin</styleUrl> 
     <LineString> 
      <tessellate>1</tessellate> 
      <coordinates> 
       81.03730709247927,15.27205299521836,0 80.99634866995486,14.75503556947802,0 
      </coordinates> 
     </LineString> 
    </Placemark> 
    <Placemark> 
     <name>1</name> 
     <styleUrl>#m_ylw-pushpin0</styleUrl> 
     <LineString> 
      <tessellate>1</tessellate> 
      <coordinates> 
       80.41501791166907,15.31966921785412,0 80.66681262872422,15.30770770315245,0 81.04469571795477,15.27884283211159,0 
      </coordinates> 
     </LineString> 
    </Placemark> 
</Folder> 

이이 방식으로 80.41501791166907,15.31966921785412 80.66681262872422,15.30770770315245 81.04469571795477,15.27884283211159XML에서 텍스트 파일에 노드 값을 추출하는 방법

에서 .I이 파일에서 좌표 값을 얻기 위해 필요한 XML 파일입니다 사용

81.03730709247927,15.27205299521836 80.99634866995486,14.75503556947802

내 코드는 오류가 표시되지 않습니다하지만 난 널 excepti을 반환하는 것 실행할 때 on

private void button33_Click_1(object sender, EventArgs e) 
    { 
     DialogResult result = openFileDialog1.ShowDialog(); 
     if (result == DialogResult.OK) 
     { 
      DialogResult result2 = folderBrowserDialog1.ShowDialog(); 
      if (result2 == DialogResult.OK) 
      { 
       ZipFile.ExtractToDirectory(openFileDialog1.FileName, folderBrowserDialog1.SelectedPath); 
       MessageBox.Show("ZIP file extracted successfully!"); 
      } 
     } 
     OpenFileDialog openFileDialog = new OpenFileDialog(); 
     openFileDialog.Multiselect = true; 
     openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; 
     openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
     if (openFileDialog.ShowDialog(this) == DialogResult.OK) 
     { 
      XNamespace xns = "http://www.opengis.net/kml/2.2"; 
      XDocument xdInput = XDocument.Load(openFileDialog.FileName); 
      XElement xeDocument = xdInput.Root.Element(xns + "Document"); 
      XElement xePlaceMark = xeDocument.Element(xns + "Placemark"); 

      XElement xeCoord = xePlaceMark.Descendants(xns + "coordinates").First(); 
     // string[] lines = Regex.Split(xeCoord.Value, @"\,\0,\ "); 
      string[] lines = xeCoord.Value.Trim().Split(new string[] {",0 "}, StringSplitOptions.RemoveEmptyEntries); 
     /*  File.WriteAllLines(@"C:\Temp\Output.txt", lines); 

       string strFilename = openFileDialog.FileName; 
       XmlDocument xmlDoc = new XmlDocument(); 
       if (File.Exists(strFilename)) 
       { 
        xmlDoc.Load(strFilename); 
        string xmlFile = File.ReadAllText(strFilename); 
        XmlDocument xmldoc = new XmlDocument(); 
        xmldoc.LoadXml(xmlFile); 
       XmlNodeList nodeList = xmldoc.GetElementsByTagName("coordinates"); 
       string coordinates = string.Empty; 
       foreach (XmlNode node in nodeList) 
       { coordinates = node.InnerText; 
        string[] lst = coordinates.Split(" ".ToCharArray()); 
        foreach (string str in lst) 
        { 
        coordinates += str + Environment.NewLine; 
       } 
       }*/ 
      SaveFileDialog savefile = new SaveFileDialog(); 
      savefile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; 
      if (savefile.ShowDialog() == DialogResult.OK) 
      //{ using (StreamWriter sw = new StreamWriter(savefile.FileName)) 
      { 
       File.WriteAllLines(savefile.FileName, lines); 
       MessageBox.Show("Long/Lat coordinates extracted successfully!"); 
       //  sw.WriteLine(lines); 
       // sw.Flush();} 
      } 

     } 


    } 

답변

0

XML 파일의 형식이 맞다고 가정하면 아래에서 필요한 데이터를 추출하여 줄 단위로 텍스트 파일에 삽입하는 것이 좋습니다.

 string pathToXmlFile = ""; 

     XDocument document = XDocument.Load(pathToXmlFile); 

     var coordinates = document.Descendants("coordinates"); 

     string pathToTextFile = ""; 

     StreamWriter streamWriter = new StreamWriter(pathToTextFile); 

     foreach (XElement coordinate in coordinates) 
     { 
      streamWriter.WriteLine(coordinate.Value); 

      Console.WriteLine(coordinate.Value); 
     } 

     streamWriter.Close(); 
관련 문제