Comma Separated Files (Read / Write) - C #

I have been very interested in CSV files for several years, but I have never had to use them until now, so I'm pretty new to this.

I have an application that stores information about the controls that were created by the user at runtime. I plan to use CSV files to store this information, which will later be retrieved by the user when they open the file in my program.

These CSV files will have the following structure. Say, for example, a user creates LinkLabel, assigns him some text, and then creates a shortcut and assigns him some text. The output file (CSV file) will look like this:

CSVFile.csv


LinkLabel,This is LinkLabel text,170,40
Label,This is Label text,170,50

Explanation:


Control,Text,LocationX,LocationY
Control,Text,LocationX,LocationY

. , , . Control, Text, LocationX, LocationY...

- , , ? ?

, .

+3
6

csv , , xml . .

+3

FileHelpers CSV . , , CSV , .

, .

+3

, xaml , ? , , csv , . xml ( , ):

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
public enum ControlType {
    Label, LinkLabel
}
[XmlRoot("controls")]
public class ControlWrapper {
    [XmlElement("control")] public List<ControlDto> Controls { get; set; }
    public ControlWrapper() {
        Controls = new List<ControlDto>();
    }
}
public class ControlDto {
    [XmlAttribute("type")] public ControlType Type { get; set; }
    [XmlAttribute("caption")] public string Caption { get; set; }
    [XmlAttribute("x")] public int LocationX { get; set; }
    [XmlAttribute("y")] public int LocationY { get; set; }
}
static class Program {
    static void Main() {
        var model = new ControlWrapper {
            Controls = {
                new ControlDto {
                    Type = ControlType.LinkLabel,
                    Caption = "This is LinkLabel text",
                    LocationX = 170, LocationY = 40
                }, new  ControlDto {
                    Type = ControlType.Label,
                    Caption = "This is Label text",
                    LocationX = 170, LocationY = 50
                }
            }
        };
        var ser = new XmlSerializer(typeof(ControlWrapper));
        ser.Serialize(Console.Out, model);
    }
}

, , . , , DataContractSerializer ( WCF), protobuf-net ( ) ..

+2

XML, JSON , , , . JSON - , , .

JSON, JSONSharp jsonsharp.

, XML () , : xml

CSV, # - csv , , , , .

, JSON:

["LinkLabel","This is LinkLabel text",170,40]
+2

, CSV , . XML , , .

, CSV :

  • , .
  • , .
  • , .
  • , , , .
0

, .

Google and read some examples.

Basically you look at creating an instance of the FileStream class and writing to disk.

I would post an example, but it’s best to first study a bit and then rescan a more specific question.

Writing to a CSV is simply writing to a text file.

-2
source

Source: https://habr.com/ru/post/1719158/


All Articles