Search code examples
c#serializationexport-to-text

Export model to text file with out column names or headers(only values)


I have a DTO(Data Transfer Object) object as model which contains data from Response received. I need to export only values under header/column to text file. Column names or header need not be exported. I am able to export data in XML format with the help of XmlSerializer. But not able to find any text serializer. My model like below :

public class ResponseGradeDto
    {
        [XmlIgnore]
        [XmlElement(ElementName = "GRADEID")]
        public Guid Id { get; set; }
        [XmlElement(ElementName = "GRADENAME")]
        public string Name { get; set; }
        [XmlElement(ElementName = "GRADECODE")]
        public string Code { get; set; }
        public List<GradeQualitySpecDto> QualitySpecItem { get; set; }

}

I tried below code :

System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(responseGradeDto.GetType());

            using (StringWriter textWriter = new StringWriter())
            {
                xmlSerializer.Serialize(textWriter, responseGradeDto);
                string a = textWriter.ToString();
                return textWriter.ToString();
            }

Suppose my Model is as below :

{

        "name": "My Name",
        "code": "1234",
        "information": "My Info",
        "gradeQualitySpecItem": [
        {
            "propertyid": "100",
            "propertyname": "PropertyName1",
            "target": 10,
            "sigma": 20
        },
        {
            "propertyid": "200",
            "propertyname": "PropertyName2",
            "target": 10,
            "sigma": 30
        }]
}

I need output in text file as below :

AL300 SAMPLE(Some hard coded text)
My Name
1234
My Info
PROP-SUMMARY
100
PropertyName1
10
20
PROP-SUMMARY
200
PropertyName2
10
30
end AL300 SAMPLE(end of file)

enter image description here

If it is list I am getting below output for list. Can any one help me in this?


Solution

  • There is no built in "text only" serializer that serializes the object propery values as line separated text. Most of the time when you want to save your object as text you just write the code to do so.

    Example:

    var x = new ResponseGradeDto{ 
            Id = Guid.NewGuid(), 
            Name = "Foo",
            Code = "Cde",
            Information = "No info"
    };
    
    using (var writer = new StreamWriter(@"C:\temp\log.txt"))
    {
        writer.WriteLine(x.Name);
        writer.WriteLine(x.Code);
        writer.WriteLine(x.Information);
    }
    

    However, a more generic way is to use reflection to get all the references properties of the object:

    var properties = typeof(ResponseGradeDto).GetProperties();
    

    Dumping the properties into a file can then be trivial (notice that I use the object x defined in code above) :

    File.WriteAllLines(@"C:\temp\attr.txt", properties.Select(p => p.GetValue(x).ToString()));
    

    If you wish you can use attributes with the reflection solution above to filter out wanted / unwanted properties. Here I reuse the "Xml attributes" that you have used in your example, you could write your own attributes:

    var properties = typeof(ResponseGradeDto).GetProperties().Where(
                        prop => Attribute.IsDefined(prop, typeof(XmlElementAttribute))
                            && !Attribute.IsDefined(prop, typeof(XmlIgnoreAttribute))
                );
    

    Hope this helps!