亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 .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 關注
  • 128 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號