如何将节点值从xml提取到文本文件

时间:2016-09-14 11:23:50

标签: c# xml c#-4.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

81.03730709247927,15.27205299521836 80.99634866995486,14.75503556947802

我使用的代码没有显示错误但是当我运行它时返回null异常

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

        }


    }

1 个答案:

答案 0 :(得分: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();
相关问题