HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Excel to serializable object

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
objectserializableexcel

Problem

I've got an Excel file from which I have to read out data to an object to serialize. So far I came up with this solution, and I'm curious if there are any clearer solutions.

```
public myClassFromXsd excelToMyClassFromXsd(String excelFile)
{
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workBook = excelApp.Workbooks.Open(excelFile);
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Sheets[1];

myClassFromXsd spec = new myClassFromXsd();
UserHeader user = new UserHeader();
ResultHeader header = new ResultHeader();
anotherClassFromXsd specOp = new anotherClassFromXsd();
List specOpList = new List();
thisIsAClassToo tcd = new thisIsAClassToo();

excelApp.Visible = false;

tcd.orderNumber = Convert.ToString(workSheet.get_Range("B3").Value);
switch (((String)workSheet.get_Range("B4").Value).ToUpper())
{
case "I":
tcd.typeOfThis = EnumFromXsd.I;
break;
case "E":
tcd.typeOfThis = EnumFromXsd.E;
break;
case "D":
tcd.typeOfThis = EnumFromXsd.D;
break;
default:
break;
}
switch (((String)workSheet.get_Range("B5").Value).ToUpper())
{
case "I":
tcd.othersCanAlter = true;
break;
case "N":
tcd.othersCanAlter = false;
break;
default:
break;
}
tcd.oneProperty = Convert.ToString(workSheet.g

Solution

The indendation looks off - assuming it's not a paste glitch with tabs vs spaces, the scope-opening brace should line up with the method's signature:

public myClassFromXsd excelToMyClassFromXsd(String excelFile)
            {


Like this:

public myClassFromXsd excelToMyClassFromXsd(String excelFile)
{


Also, C# type names should be PascalCase, as well as any public members:

  • myClassFromXsd should be MyClassFromXsd



  • serializeXml should be SerializeXml



  • deserializeXml should be DeserializeXml



The get_Xxxx methods in the COM Interop interface aren't following this convention, but they're COM Interop methods, with their own conventions - they're not an example to follow for typical C# code.

I like that you're wrapping your StreamWriter in a using block. I don't understand why you're not doing the same with the StreamReader in the deserializeXml method - the reader.Close() call could then be removed:

public myClassFromXsd deserializeXml(String xmlFile)
    {
        myClassFromXsd mcfx = new myClassFromXsd();
        XmlSerializer serializer = new XmlSerializer(typeof(myClassFromXsd));
        using (StreamReader reader = new StreamReader(xmlFile))
        {
            mcfx = (myClassFromXsd)serializer.Deserialize(reader);
        }
        return mcfx;
    }


I would give mcfx a meaningful name: as it stands it looks like it's just a shortened version of the return type's name - I like having a result for these kinds of things. Identifiers should have a pronounceable name that carries their meaning.

Code Snippets

public myClassFromXsd excelToMyClassFromXsd(String excelFile)
            {
public myClassFromXsd excelToMyClassFromXsd(String excelFile)
{
public myClassFromXsd deserializeXml(String xmlFile)
    {
        myClassFromXsd mcfx = new myClassFromXsd();
        XmlSerializer serializer = new XmlSerializer(typeof(myClassFromXsd));
        using (StreamReader reader = new StreamReader(xmlFile))
        {
            mcfx = (myClassFromXsd)serializer.Deserialize(reader);
        }
        return mcfx;
    }

Context

StackExchange Code Review Q#80580, answer score: 2

Revisions (0)

No revisions yet.