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

拉梅什·安南斯(Ramesh Ananth)
    <?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();}
            }

        }


    }
巴维克·拉姆松达(Bhavik Ramsundar)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将 Windows 服务列表及其状态提取到文本文件中?

来自分类Dev

如何将文本文件读取到DataTable

来自分类Dev

如何将多个文本文件中的特定行提取到Linux上的单个txt文件中?

来自分类Dev

如何将时间从文本文件提取到另一个文本文件/列表中

来自分类Dev

将列从csv提取到文本文件

来自分类Dev

将html文本文件提取到文本文件

来自分类Dev

如何将数据从文本文件读取到结构数组中

来自分类Dev

我如何将Linux centos中的树样式目录获取到文本文件

来自分类Dev

如何将多个文本文件读取到pyspark中的数据框中

来自分类Dev

Powershell-如何将目录中所有文本文件的第一行提取到单个输出文件中?

来自分类Dev

如何将基于一列中不同类别的数据行提取到单独的文本文件中?

来自分类Dev

将数据从文本文件提取到输出文件

来自分类Dev

如何从文本文件中提取XML

来自分类Dev

Python:如何将包含行列格式的坐标的文本文件读取到xy坐标数组中?

来自分类Dev

如何将具有浮点数的文本文件读取到C中的浮点数组

来自分类Dev

如何将结构化文本文件转换为xml文件api

来自分类Dev

拆分多个嵌套键,值并提取到文本文件

来自分类Dev

如何从文本文件中提取特定的值/字段?

来自分类Dev

如何从文本文件中提取键和值

来自分类Dev

从MATLAB仅将数值数据从文本文件中提取到矩阵中

来自分类Dev

将文本文件中的数据提取到VBA中的Excel中

来自分类Dev

使用Powershell将多行文本文件提取到单行csv

来自分类Dev

将文本文件中的数据提取到VBA中的Excel中

来自分类Dev

使用Shell脚本将文本文件提取到变量

来自分类Dev

将文本文件提取到SQL Server 2008表

来自分类Dev

使用Bash,Perl和Regex将文本文件中的变量提取到数组中

来自分类Dev

将文本文件的前5行提取到变量中

来自分类Dev

如何将文本文件中的值存储在Java数组中?

来自分类Dev

如何将值附加到empy文本文件中

Related 相关文章

  1. 1

    如何将 Windows 服务列表及其状态提取到文本文件中?

  2. 2

    如何将文本文件读取到DataTable

  3. 3

    如何将多个文本文件中的特定行提取到Linux上的单个txt文件中?

  4. 4

    如何将时间从文本文件提取到另一个文本文件/列表中

  5. 5

    将列从csv提取到文本文件

  6. 6

    将html文本文件提取到文本文件

  7. 7

    如何将数据从文本文件读取到结构数组中

  8. 8

    我如何将Linux centos中的树样式目录获取到文本文件

  9. 9

    如何将多个文本文件读取到pyspark中的数据框中

  10. 10

    Powershell-如何将目录中所有文本文件的第一行提取到单个输出文件中?

  11. 11

    如何将基于一列中不同类别的数据行提取到单独的文本文件中?

  12. 12

    将数据从文本文件提取到输出文件

  13. 13

    如何从文本文件中提取XML

  14. 14

    Python:如何将包含行列格式的坐标的文本文件读取到xy坐标数组中?

  15. 15

    如何将具有浮点数的文本文件读取到C中的浮点数组

  16. 16

    如何将结构化文本文件转换为xml文件api

  17. 17

    拆分多个嵌套键,值并提取到文本文件

  18. 18

    如何从文本文件中提取特定的值/字段?

  19. 19

    如何从文本文件中提取键和值

  20. 20

    从MATLAB仅将数值数据从文本文件中提取到矩阵中

  21. 21

    将文本文件中的数据提取到VBA中的Excel中

  22. 22

    使用Powershell将多行文本文件提取到单行csv

  23. 23

    将文本文件中的数据提取到VBA中的Excel中

  24. 24

    使用Shell脚本将文本文件提取到变量

  25. 25

    将文本文件提取到SQL Server 2008表

  26. 26

    使用Bash,Perl和Regex将文本文件中的变量提取到数组中

  27. 27

    将文本文件的前5行提取到变量中

  28. 28

    如何将文本文件中的值存储在Java数组中?

  29. 29

    如何将值附加到empy文本文件中

热门标签

归档