为了账号安全,请及时绑定邮箱和手机立即绑定

如何将 .tsv 内容转换为 xml

如何将 .tsv 内容转换为 xml

C#
qq_花开花谢_0 2022-12-31 11:21:06
我有一个tsv如下所示的文件Time    Object  pmPdDrb pmPdcDlSrb00:45   EUtranCellFDD=GNL02294_7A_1 2588007 162600:45   EUtranCellFDD=GNL02294_7B_1 18550   3200:45   EUtranCellFDD=GNL02294_7C_1 26199   3800:45   EUtranCellFDD=GNL02294_9A_1 3857243 751是否可以像下面这样将其转换为 XML?<xmlnode>  <Time>00:45</Time>  <Object>EUtranCellFDD=GNL02294_7A_1</Object>  <pmPdDrb>2588007</pmPdDrb>  <pmPdcDlSrb>1626</pmPdcDlSrb></xmlnode>我试过下面的代码:var reader = File.ReadAllLines(logFile);var xml1 = new XElement("TopElement",reader.Select(line => new XElement("Item",    line.Split('\t').Select((column, index) =>         new XElement("Column" + index,column)))    ));
查看完整描述

1 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

尝试以下:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml;

using System.Xml.Linq;

using System.IO;


namespace ConsoleApplication110

{

    class Program

    {

        const string INPUT_FILENAME = @"c:\temp\test.txt";

        const string OUTPUT_FILENAME = @"c:\temp\test.xml";

        static void Main(string[] args)

        {

            string header = "<xmlnodes></xmlnodes>";


            XDocument doc = XDocument.Parse(header);

            XElement xmlnodes = doc.Root;


            StreamReader reader = new StreamReader(INPUT_FILENAME);


            string line = "";

            string[] columnNames = null;

            int lineCount = 0;

            while((line = reader.ReadLine()) != null)

            {

                line = line.Trim();

                if (line.Length > 0)

                {

                    string[] splitArray = line.Split(new char[] { '\t', ' '}, StringSplitOptions.RemoveEmptyEntries);

                    if (++lineCount == 1)

                    {

                        columnNames = splitArray;

                    }

                    else

                    {

                        XElement newNode = new XElement("xmlnode");

                        xmlnodes.Add(newNode);

                        for(int i = 0; i < splitArray.Length; i++)

                        {

                            XElement xColumn = new XElement(columnNames[i], splitArray[i]);

                            newNode.Add(xColumn);

                        }


                    }

                }

            }


            doc.Save(OUTPUT_FILENAME);


        }


    }



}



查看完整回答
反对 回复 2022-12-31
  • 1 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信