C# 复杂文本文件到XML文件的转换

C# 复杂文本文件到XML文件的转换,c#,xml,schema,C#,Xml,Schema,我想用以下.txt文件创建一个XML文件 以下对象具有相同的结构。可以创建XML吗 我不需要.txt的全部内容。只有“信息对象X、增量X、增量Y、增量Z、所有旋转、标注和零件类型”才是重要和必要的 在最好的情况下,我希望直接在C#程序中转换这些文件 谢谢你的帮助 ============================================================ Created by : Muster Date

我想用以下.txt文件创建一个XML文件


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

我不需要.txt的全部内容。只有“信息对象X、增量X、增量Y、增量Z、所有旋转、标注和零件类型”才是重要和必要的

在最好的情况下,我希望直接在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)

根据要求,这里是读卡器的一部分,用于读取Delta X、Y和Z。 您可以读取该行并检查它是否以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和其他您想阅读的行。如果您有任何问题,请将代码添加到问题中,人们将尽可能提供帮助。

是否可以创建XML?对您可以解析文件中所需的内容,然后将其写入xml。你试过什么了吗?我还是试着把它转换成XML文件。我不可能只解析我想要的内容,也无法为我的XML创建正确的结构。你有没有给我举个例子,如何选择我想要的内容并将其插入XML?。您可以阅读所需的行,并创建一个表示xml数据结构的对象,然后将其写入xml。谢谢,这将对我有所帮助。我想我的例子和这个有点不同。如果你给我举个例子,可能是关于“Delta X”或“PART_TYPE”行,会有问题吗?这将是非常有帮助的米你在寻找一种方式来理解每一行的实际内容?试着看看正则表达式这个主题。