【问题标题】:Complex text file to XML-File transformation复杂的文本文件到 XML 文件的转换
【发布时间】:2018-04-26 00:02:19
【问题描述】:

我想创建一个带有以下 .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)

【问题讨论】:

  • 是否可以创建 XML?是的。您可以解析文件以获取所需的内容,然后将其写入 xml。你有没有尝试过?
  • 我仍然尝试将其转换为 XML 文件。我不可能只解析我想要的内容,也无法为我的 XML 创建正确的结构。你有我的例子,我如何选择我想要的内容并将其插入到 XML 中?
  • Here is a question that shows how to parse a text file。您可以读取所需的行并创建一个表示 xml 数据结构的对象,然后可以将其写入 xml。
  • 谢谢,它会帮助我一点点。我认为我的示例与此有些不同。如果你给我一个例子,可能是“Delta X”或“PART_TYPE”行,这会是一个问题吗?这对我很有帮助
  • 您是否正在寻找一种方法来了解每一行的实际内容?尝试查看正则表达式的主题。

标签: c# xml schema


【解决方案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 和您想阅读的其他行。如果您有任何问题,请将代码添加到问题中,人们会尽其所能提供帮助。

【讨论】:

    猜你喜欢
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    相关资源
    最近更新 更多