Writing Information From an XML to a CSV File

Experiences, small talk, and other automation gossip.
Ben Jardine
Posts: 4
Joined: Thu Oct 08, 2015 9:51 am

Writing Information From an XML to a CSV File

Post by Ben Jardine » Thu Oct 08, 2015 10:19 am

I'm trying to write some code which writes a number from an XML file to a CSV file which can be referenced in later tests. I would like the output to look like this, the text PolicyNumber is a header and then the 1234... number is the output policy number:

PolicyNumber
1234PC1234


I have managed to get it to output the information to the CSV but it looks more like this:

PolicyNumber
---Blank Line---
1234PC1234
---Blank Line---


I have three cs files to create this which are attached, I believe it has something to do with the CreateCSVFile code because if I enter:

writer.WriteLine("PolicyNumber");
writer.WriteLine("");
writer.Flush();
writer.Close();

Then the output has the second example but if I change the top line to writer.Write("PolicyNumber"); then the output looks like:

PolicyNumber
---Blank Line---


It also looks the same if I remove writer.WriteLine("");

The ideal output would have PolicyNumber as the header and then a policy number on the line below and if I ran the test again it would put the next policy number on the line below the first one.

Thanks!
Attachments
SetDataSource.cs
C# Code To Set The Data Source
(2.52 KiB) Downloaded 87 times
MineDataFromOutputXML.cs
C# Code To Mine Data From The XML File
(2.97 KiB) Downloaded 73 times
CreateCSVFile.cs
C# Code To Create The CSV File
(1.72 KiB) Downloaded 91 times

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Writing Information From an XML to a CSV File

Post by odklizec » Fri Oct 09, 2015 9:25 am

Hi,

I think your problem is caused by the fact that WriteLine writes specified string with "CR LF" characters at the end of entered string. This is expect behavior of writing line ;) If I run your CreateCSVFile sample multiple times, it correctly saves the csv file with empty line only at the end of file, not between the lines as in your example? So the question is, how exactly do you call the WriteLine in your code? Could you please post the entire solution/sample project?

BTW, I'm personally using Ranorex Data Connector functionality to write strings to CSV files. Here is a quick & dirty sample code...
//create CSV data connector
string connector = "CSVConnector";
Ranorex.Core.Data.CsvDataConnector csvConnector = new Ranorex.Core.Data.CsvDataConnector(connector,csvOutputPath,true);
csvConnector.SeparatorChar = ',';
Ranorex.Core.Data.ColumnCollection propTableColumnsCSV = new Ranorex.Core.Data.ColumnCollection();
Ranorex.Core.Data.RowCollection propTableRowsCSV = new Ranorex.Core.Data.RowCollection(propTableColumnsCSV);
//create CSV column headers
propTableRowsCSV.Add(new string[3]{"ElementName","ElementIndex","ElementValue"});
propTableRowsCSV.Add(new string[3]{tagName,"1",tagValue});
csvConnector.StoreData(propTableColumnsCSV, propTableRowsCSV);
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Ben Jardine
Posts: 4
Joined: Thu Oct 08, 2015 9:51 am

Re: Writing Information From an XML to a CSV File

Post by Ben Jardine » Fri Oct 09, 2015 3:11 pm

Hi,

Thanks for your reply, I will compile a project and get it over to you on Monday.

Ben Jardine
Posts: 4
Joined: Thu Oct 08, 2015 9:51 am

Re: Writing Information From an XML to a CSV File

Post by Ben Jardine » Wed Oct 14, 2015 2:41 pm

Hi, apologies for the slightly late reply, I have compiled a project with my set up that I'm using, I've also attached the output file that is generated by our program, I've cut it down so it just has the policy number. If you need the location on the computer where this goes it is C:\Users\[username]\AppData\Local\Temp.

As seen in the screenshot each one of these test cases contains the create, data mine and set data source files. What I would like it to do would be create the initial CSV file at the start of the main test case with a header saying 'policy number'. When the first policy is created it should add the policy number to the CSV file on a new line below the header. When the second policy is created it should add the second policy number on a line below the first policy number. It should do this for all six policies created so by the end we should have a CSV file with 6 policy numbers underneath the policy number header.
Image
Attachments
CSPolicyNumbersToCSVFile.zip
ZIP file containing my project and output.xml
(17.57 KiB) Downloaded 71 times

jma
Posts: 111
Joined: Fri Jul 03, 2015 9:18 am

Re: Writing Information From an XML to a CSV File

Post by jma » Mon Oct 19, 2015 7:36 am

Hello Ben,

Unfortunately, I was not able to reproduce the issue on my machine with the attached solution.

If the following call of the WriteLine()-method is disabled within 'CreateCSVFile.cs', no empty line is added between the values.
CreateCSVFile.png
CreateCSVFile.png (2.93 KiB) Viewed 1622 times
CSVFile_result.csv.png
CSVFile_result.csv.png (2.05 KiB) Viewed 1622 times
The empty line seems to be created by 'writer.WriteLine("")';
Could you try again running your code without calling the additional WriteLine()-method?

Ben Jardine
Posts: 4
Joined: Thu Oct 08, 2015 9:51 am

Re: Writing Information From an XML to a CSV File

Post by Ben Jardine » Mon Oct 19, 2015 9:39 am

Hi,

I have removed the blank writeline and when I try to generate the csv it has the PolicyNumber header and just a blank line beneath it that hasn't been populated?

Thanks
Ben

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Writing Information From an XML to a CSV File

Post by Support Team » Thu Oct 22, 2015 3:45 pm

Hi Ben,

If the issue isn't solved by now, please contact [email protected].

We'd be happy to assist you in this matter.

Regards,
Johannes
.
Image