复杂文本文件到XML文件转换

时间:2017-11-13 10:23:06

标签: c# xml schema

我想用以下.txt。

创建一个XML文件

以下对象具有相同的结构。是否可以创建XML?

我不需要.txt的全部内容。只有“信息ObjectX,Delta X,Delta Y,Delta Z,所有旋转,CALLOUT和PART_TYPE”都是重要且必要的。

最好的,我想直接在C#程序中转换这些文件。

感谢您的帮助!

 ============================================================
Created by             : Muster
Date                           :  13.11.2017 10:10:10
Activated component            : File
Node name                      :  abc123abc
============================================================
Information Object # 1

Name                 Object1
File-Path        FilePath
Component-Path       Component-Path
Typ                  Component

Color                2 (Green)
Component-Typ        Body
Version          301   01 Jan 2017 10:10 (Created by Muster)

Object is visible

Positions:

Delta X              =   5.00000000010
Delta Y              =   16.0000000001
Delta Z              =   20.0000000200

Rotations:

X-vector             XC =    0.0                  X =    0.0              
                     YC =    1.0                  Y =    1.0              
                     ZC =    0.0                  Z =    0.0              

Y-vector             XC =   -1.0                  X =   -1.0              
                     YC =    0.0                  Y =    0.0              
                     ZC =    0.0                  Z =    0.0              

Z-vektor             XC =    0.0                  X =    0.0              
                     YC =    0.0                  Y =    0.0              
                     ZC =    1.0                  Z =    1.0              

Component is xxx
3 Rotations- and Translations available

------------------------------------------------------------


------------------------------------------------------------

CALLOUT    = 4890 (string)
PART_NAME = body head (string)
PART_TYPE = Item (string)

************************************************************

Information Object # 2

Name                 Object2
File-Path        FilePath
Component-Path       Component-Path
Typ                  Component

Color                2 (Green)
Component-Typ        Body
Version          301   01 Jan 2017 10:10 (Created by Muster)

Object is visible

Positions:

Delta X              =   5.00000000010
Delta Y              =   25.0000000000
Delta Z              =   20.000000200

Rotations:

X-vector             XC =    0.0                  X =    0.0              
                     YC =    1.0                  Y =    1.0              
                     ZC =    0.0                  Z =    0.0              

Y-vector             XC =   -1.0                  X =   -1.0              
                     YC =    0.0                  Y =    0.0              
                     ZC =    0.0                  Z =    0.0              

Z-vektor             XC =    0.0                  X =    0.0              
                     YC =    0.0                  Y =    0.0              
                     ZC =    1.0                  Z =    1.0              

Component is xxx
3 Rotations- and Translations available

------------------------------------------------------------
------------------------------------------------------------

CALLOUT    = 4891 (string)
PART_NAME = body head (string)
PART_TYPE = Item (string)

1 个答案:

答案 0 :(得分:-1)

根据要求,读者可以阅读Delta X,Y& Z. 您可以阅读该行并检查它是否为StartsWith Delta。您可以Split等号上的行和Trim字符串,然后解析为十进制值。然后,您可以对其他Delta值重复此操作。

//values to store the delta values in
decimal deltaX, deltaY, deltaZ;
//The reader for the text file
using (var reader = new StreamReader("filename.txt"))
{
    string line;
    //read file line by line
    while ((line = reader.ReadLine()) != null)
    {

        //Check if line starts with Delta 
        if (line.StartsWith("Delta"))
        {
            //For this line you can split by  '=' and then read the second value in the array, trim the string and parse to a decimal value
            deltaX = decimal.Parse(line.Split(new[] {'='})[1].Trim());
            //move reader to next line as we know it will be DeltaY & repeat
            reader.ReadLine();
            deltaY = decimal.Parse(line.Split(new[] { '=' })[1].Trim());
            reader.ReadLine();
            deltaZ = decimal.Parse(line.Split(new[] { '=' })[1].Trim());
        }
    }
}

随意浏览Part_X和您想要阅读的其他行。如果您有任何问题,请在问题中添加代码,人们将尽可能地提供帮助。