2016-09-14 79 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> 

這是XML文件。我需要從這個文件中獲取的座標值,以這種方式 80.41501791166907,15.31966921785412 80.66681262872422,15.30770770315245 81.04469571795477,15.27884283211159如何提取節點值從XML到文本文件

使用

81.03730709247927,15.27205299521836 80.99634866995486,14.75503556947802

我的代碼沒有顯示錯誤,但是當我運行它返回null excepti在

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