2

Is there any component which would take a data object, and output it to CSV-format via reflection, based on some rules ?

For example, take the below data structure:

Member
------
Name : string
Surname : string                         
ContactDetails : ContactDetails

ContactDetails
--------------
Address1 : string
Address2 : string
City : string
Telephone : string

If one had to export the Member to a CSV, it would result in several columns as below:

  • Name
  • Surname
  • ContactDetails.Address1
  • ContactDetails.Address2
  • ContactDetails.City
  • ContactDetails.Telephone

This could also be done in reverse, whereby the CSV file is converted back to data objects via reflection.

1 Answer 1

1

OK this is what I would do to create the CSV base on reflection. I would serialize the object to XML and then the resulting XML converted using XSLT to CSV:

 namespace ConsoleApplication1
    {
       [XmlRoot(ElementName = "Players")] 
        public class Players : System.Collections.Generic.List<Player> {
            public Players() { }
        }

       public class Player{
       public string Name { get; set; }
           public string Team { get; set; } 
           public string Position { get; set; }
           public Player(string name, string position, string team) {
               this.Name = name;
               this.Position = position;
               this.Team = team;
           }
           public Player() { }
       }


        [System.Runtime.InteropServices.GuidAttribute("D36900FE-8902-4ED8-B961-DE5B3F3273AC")]
        class Program
        {
            static void Main(string[] args)
            {
                Players players= new Players();
                players.Add(new Player("C.Ronaldo", "Man Utd", "Midfielder"));
                XmlSerializer ser = new XmlSerializer(typeof(Players));
                var writer = new System.IO.StreamWriter(@"C:\myxml.xml", false);
                ser.Serialize(writer, players);
                writer.Flush();
                writer.Close();            
                var myXslTrans = new XslCompiledTransform();
                myXslTrans.Load(@"C:\CSV.xslt", null, null );            
                myXslTrans.Transform(@"file://C:/myxml.xml", @"C:\converted.csv"); 
            }
        }
    }

This is my sample XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:telerik="remove" xmlns:asp="remove">
    <xsl:template match="/">        
    <xsl:for-each select="Players/Player">
        <xsl:value-of select="Name" />,<xsl:value-of select="Team" />,<xsl:value-of select="Position" />
    </xsl:for-each>     
    </xsl:template>
</xsl:stylesheet>

Disclaimer:

  I know the XSLT needs break lines or may need additional formats. I only intent to describe a possible path to CSV/Reflection and not a fully working solution.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.