简体   繁体   中英

Create XML from CSV using LINQ and XELEMENT

i have to create an xml file from csv file. Actually i use this code:

XElement header = new XElement("header",
    from str in source
    let fields = str.Split('|')
    select new XElement("Header",
        //new XAttribute("CustomerID", fields[0]),
        new XElement("FileId", fields[1]),//da calcolare
        new XElement("SenderId", fields[2]),
        new XElement("ProcessingDate", DateTime.Now.ToString("yyyyMMdd")),
        new XElement("ProcessingTime", DateTime.Now.ToString("HHmm"))
        )
    );

This creates 2 tags: "header xmlns=""" and Header ; how can i create just one tag "Header"?

Output:

<header xmlns="">
 <Header>
 <FileId>00476170246</FileId>
 <SenderId>TEST</SenderId>
 <ProcessingDate>20210819</ProcessingDate>
 <ProcessingTime>1825</ProcessingTime>
 </Header>
</header>

LINQ is made to work with collections.

If you want to create but one element, it makes no sense to use Linq the way you do.

What you do in your code ist:

You manually create an Header element with new XElement(..) , and then let LINQ fill that element with as many headers are in the source collection, by telling it from every source it should split the text and then select a new Header.

So, that why the result looks exactly like what you describe:

<YOUR Header (from new)>
  <LINQ CREATED HEADER (from select new(...))
  [<LINQ CREATED HEADER (from select new(...), when source has more entries)]
  [<LINQ CREATED HEADER (from select new(...), when source has more entries))]
</YOUR Header>

look at that:

https://do.netfiddle.net/f2Uo8Y

using System.Collections.Generic;
using System;
using System.Linq;
using System.Xml.Linq;
using Newtonsoft.Json;
                    
public class Program
{
    public static void Main()
    {
        var source = new List<string>();
        source.Add("fid|sid|pd|pt");
        
        //2. comment this in and see 
        //source.Add("fid2|sid2|pd2|pt2");
        
        //1. when you do a FROM, you receive collections back, not single objects
        var headerElements = from str in source
            let fields = str.Split('|')
            select new XElement("Header",
                new XElement("FileId", fields[1]),//da calcolare
                new XElement("SenderId", fields[2]),
                new XElement("ProcessingDate", DateTime.Now.ToString("yyyyMMdd")),
                new XElement("ProcessingTime", DateTime.Now.ToString("HHmm"))
        );
    
        Console.WriteLine(JsonConvert.SerializeObject(headerElements));
    
        //now, if you want only one element, you could assume that source has only one entry, and crop the result 
        var headerElement = headerElements.FirstOrDefault();
        Console.WriteLine(JsonConvert.SerializeObject(headerElement));
    
        //BUT: in that case, solving that via LINQ makes absolutely no sense
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM