Skip to content

Commit

Permalink
Added patch to CsvToXml to support cases where the RecordDelimiter is…
Browse files Browse the repository at this point in the history
… the last character on the line.
  • Loading branch information
jprichardson committed Oct 19, 2011
1 parent 689e00e commit 5ef2160
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
14 changes: 13 additions & 1 deletion CommonLib/Data/Csv/CsvToXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,19 @@ public class CsvToXml
}

public void Convert() {
var tempLines = File.ReadAllLines(this.CsvFile);
var tempFileLines = new List<string>();
var sr = new StreamReader(this.CsvFile);
var sb = new StringBuilder(255);
while (!sr.EndOfStream) {
sb.Append(sr.ReadLine());
if (sb[sb.Length - 1] == this.RecordDelimiter)
sb.Remove(sb.Length - 1, 1);
tempFileLines.Add(sb.ToString());
sb.Clear();
}
sr.Close();

var tempLines = tempFileLines.ToArray();
string[] lines = null;
_columnNames = null;

Expand Down
44 changes: 44 additions & 0 deletions TestCommonLib/Data/Csv/CsvToXmlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,50 @@ public void MyTestCleanup()
Assert.AreEqual(xml, actualXml);
}

[TestMethod()]
public void Convert2Test() {

var columnNames = "Product;Price;DateStocked;";
var data = "Pepsi;4.50;2010-05-04;\nCoke;3.00;2010-09-22;\nCheetos;7.25;2009-01-13;";

var csvWithColumnNamesFile = TestEnvironment.DataPath + "csv-columnNames.csv";
var csvFile = TestEnvironment.DataPath + "csv.csv";

File.WriteAllText(csvWithColumnNamesFile, columnNames + "\n" + data);
File.WriteAllText(csvFile, data);

string xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";

string xmlWithColumnNames = xmlHeader +
@"<Records>
<Record>
<Product>Pepsi</Product>
<Price>4.50</Price>
<DateStocked>2010-05-04</DateStocked>
</Record>
<Record>
<Product>Coke</Product>
<Price>3.00</Price>
<DateStocked>2010-09-22</DateStocked>
</Record>
<Record>
<Product>Cheetos</Product>
<Price>7.25</Price>
<DateStocked>2009-01-13</DateStocked>
</Record>
</Records>";

var csvWithColumnNames = new CsvToXml(csvWithColumnNamesFile);
csvWithColumnNames.RecordDelimiter = ';'; csvWithColumnNames.TextQualifier = null;
csvWithColumnNames.HasColumnNames = true;
csvWithColumnNames.Convert();

string actualXmlWithColumnNames = csvWithColumnNames.XmlString;

Assert.AreEqual(xmlWithColumnNames, actualXmlWithColumnNames);

}

[TestMethod()]
public void RecordsTest() {
var columnNames = "'Product','Price','DateStocked'";
Expand Down
2 changes: 1 addition & 1 deletion TestCommonLib/Mvvm/MessengerCenterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class MessengerCenterTest
}

[TestMethod()]
public void MessengerCenterTest() {
public void MessengerCenterObjTest() {
var mc = new MessengerCenter();

mc.Subscribe(this, "msg1", (msg) =>
Expand Down

0 comments on commit 5ef2160

Please sign in to comment.