如何从XML标记中获取值,并在文本文件的末尾添加新行和计数保存到文本文件中

本文关键字:文本 文件 添加 新行 保存 XML 获取 | 更新日期: 2023-09-27 18:06:37

   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>

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

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组,依此类推

我需要做的是计算第一个集合的行数并写在第一个集合的顶部,再计算第二组的行数并写在第二组的顶部,然后它应该继续提取整个坐标标签集。

0/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-除空格和0外的行数。你们能帮帮我吗

如何从XML标记中获取值,并在文本文件的末尾添加新行和计数保存到文本文件中

您可能需要两个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

试试这个

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