linq select without creating xelement

olofsan 1 Reputation point
2020-12-08T13:10:33.07+00:00

I got a problem with Linq while using select to read out fields from a csv file.
The program should read out the csv file and write it as a xml file in a specific form.
I cannot get it to work the way i like.

string[] source = File.ReadAllLines(@"PathTo.CSV");
        XElement order = new XElement("ORDER",
            from str in source
            let fields = str.Split(';')

                select new XElement("NOTTHIS",
                   new XElement("ORDERNUMBER", fields[0]),
                   new XElement("ORDERDATE", fields[10]),
                   new XElement("ORDERMARK", fields[15]),

                   new XElement("CUSTOMER",
                      new XElement("FIRSTNAME", fields[3]),
                      ...
                      new XElement("TELEPHONE", fields[9])
                )
            )
        );
order.Save(FileName);

So this is the code that works. It produces this structure in the output file:

<ORDER>
  <NOT-THIS>
    <ORDERNUMBER>A</ORDERNUMBER>
    <ORDERDATE>B</ORDERDATE>
    <ORDERMARK>C</ORDERMARK>
    <CUSTOMER>
      <FIRSTNAME>D</FIRSTNAME>
      <TELEPHONE>E</TELEPHONE>
    </CUSTOMER>
  </NOT-THIS>
</ORDER>

But the Structure i want is this:

<ORDER>
    <ORDERNUMBER>A</ORDERNUMBER>
    <ORDERDATE>B</ORDERDATE>
    <ORDERMARK>C</ORDERMARK>
    <CUSTOMER>
      <FIRSTNAME>D</FIRSTNAME>
      <TELEPHONE>E</TELEPHONE>
    </CUSTOMER>
</ORDER>

how can i get rid of the NOT-THIS while still being able to use select and fields[] ?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,363 questions
0 comments No comments
{count} votes