简体   繁体   English

使用 LINQ 和 XELEMENT 从 CSV 创建 XML

[英]Create XML from CSV using LINQ and XELEMENT

i have to create an xml file from csv file.我必须从 csv 文件创建一个 xml 文件。 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 ;这会创建 2 个标签: "header xmlns="""Header how can i create just one tag "Header"?我怎样才能只创建一个标签“Header”?

Output: 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. LINQ 与 collections 配合使用。

If you want to create but one element, it makes no sense to use Linq the way you do.如果您只想创建一个元素,那么按照您的方式使用 Linq 是没有意义的。

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.您使用new XElement(..)手动创建一个 Header 元素,然后让 LINQ 用source集合中尽可能多的标题填充该元素,方法是from每个源告诉它应该拆分文本,然后select一个新的 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 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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