如何从 xml 标签获取值并保存在文本文件中,文本文件末尾有新行以及计数

how to obtain values from xml tag and save in text file with new line at the end of the text file and also number of counts

   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 = xeCoord.Value.Trim().Split(new string[] { ",0 "

            }, StringSplitOptions.RemoveEmptyEntries);

           File.AppendAllLines(@"C:\New folder\longlat.txt", lines);

                    }

在此代码中,它提取 xml 文件并保存来自

的值
<coordinates>
80.41501791166907,15.31966921785412,80.66681262872422,15.30770770315245,81.04469571795477,15.27884283211159,0 
</coordinates>

这些标签并将其保存在 longlat 文本文件中。像这样它保存了其他文档中的其他坐标,我的最终输出看起来像

80.41501791166907,15.31966921785412
80.66681262872422,15.30770770315245
81.04469571795477,15.27884283211159,0
81.03730709247927,15.27205299521836
80.99634866995486,14.75503556947802,0

前 3 个是第 1 组,下 2 个是第 2 组,依此类推...

我需要做的是计算第一组中的行数并写在第一组之上,再次计算第二组写在第二组之上的行数,然后它应该继续整个系列提取坐标标签。

o/p我要获取的就是这种方式

3 0

80.41501791166907,15.31966921785412
80.66681262872422,15.30770770315245
81.04469571795477,15.27884283211159

2 0

81.03730709247927,15.27205299521836
80.99634866995486,14.75503556947802

等....

3- 行数以及 space 和 0 。你们能帮帮我吗

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.csv";
        static void Main(string[] args)
        {
            StreamWriter writer = new StreamWriter(FILENAME);
            string input = "<coordinates>80.41501791166907,15.31966921785412,80.66681262872422,15.30770770315245,81.04469571795477,15.27884283211159,0 </coordinates>";
            XDocument doc = XDocument.Parse(input);
            string[] groups = ((string)doc.Element("coordinates")).Trim().Split(new string[] {",0"}, StringSplitOptions.RemoveEmptyEntries);
            Boolean first = true;
            foreach (string group in groups)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    writer.WriteLine();
                }

                string[] coordinates = group.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                writer.WriteLine("{0} {1}", coordinates.Length / 2, 0);
                writer.WriteLine();
                for (int i = 0; i < coordinates.Count() - 1; i += 2)
                {
                    writer.WriteLine("{0},{1}", coordinates[i], coordinates[i + 1]);
                }
            }
            writer.Flush();
            writer.Close();

        }
    }
}

您可能需要两个 Splits,一个 ",0",然后一个 ","

var lines = xeCoord.Value.Trim()
    .Split(new string[] {",0"}, StringSplitOptions.RemoveEmptyEntries)
    .Select(x=> 
        {   
            int i=0;
            var splits = x.Split(new string[] {","},StringSplitOptions.RemoveEmptyEntries)       
                .GroupBy(g=> i++/2)                       //Group for consecutive values to form a coordinate.
                .Select(s=>string.Join(",",s))            // Join them
                .ToList();                  
            splits.Insert(0,splits.Count().ToString());   // Insert the count at the beginning
            return splits;

        })
        .SelectMany(x=>x);                                // flatten the structure

 // now write these lines to file.
 File.AppendAllLines(@"C:\New folder\longlat.txt", lines);  

勾选这个Demo