Dear all,
I try to export multi records to a CSV file in UTF-8 without BOM.
i used this code:
System.Text.Encoding encoding = new System.Text.UTF8Encoding(false); ; System.IO.File::WriteAllText(@'C:\test.txt', "Data 123", encoding);
Is this right? And How can i export multi records with break line?
Please give me a advice. Thank you so much.
*This post is locked for comments
Great!, solved.
Solution for this would be directly write the UTF-8 without BOM file using code where lobValue is a string data instead of trying to rewrite the file to convert to a file without BOM
System.IO.File::WriteAllText(textIoPathFileName,lobValue , encoding);
The line "System.IO.File::WriteAllText(filename, System.IO.File::ReadAllText(filename), encoding);" doesn't seem to work in batch it throws error In batch resulting in the file is being saved with BOM. On client the WriteAll method rewrites the file without BOM.
Error message: “Can’t rewrite <C:\Testing FOlder\Studio_Matrix.csv> file without BOM”
I am I closing the file before it is rewritten
Do you mean that WriteAllText() throws an exception? If so, what's the type and message? Isn't the file already open?
Did anyone got this working in batch mode for AX 2012. I get below error in batch mode
“Can’t rewrite C:\Testing FOlder\Studio_Matrix.csv file without BOM”
Dear Ievgen Miroshnikov,
Thank you so much. I try your code and it run perfectly.
AX cannot create file without BOM, however, using you code you can resave it.
#File CommaTextIo commaTextIo; FileIOPermission permission; CustTable custTable; str fileName = @"c:\test.csv"; System.Text.Encoding encoding = new System.Text.UTF8Encoding(false); permission = new FileIOPermission(fileName,#io_write); permission.assert(); commaTextIo = new CommaTextIo(fileName,#io_write, 65001);
while select custTable { commaTextIo.write(custTable.AccountNum); } CodeAccessPermission::revertAssert(); commaTextIo = null; //save CSV without BOM new InteropPermission(InteropKind::ClrInterop).assert(); System.IO.File::WriteAllText(filename, System.IO.File::ReadAllText(filename), encoding); CodeAccessPermission::revertAssert();
Thank you.
But it's only utf-8. I want to export CSV utf8 without BOM.
Hi Hiep Nguyen,
You can use standard AX class commaTextIo to create CSV files, please refer to this blog post fro example
http://daxdilip.blogspot.co.nz/2011/08/commatextio-class-how-to-export-data-to.html
As you can see commaTextIo.write() is called in a loop to insert multiple records.
To set encoding you can use 3 parameter of commaTextIo constructor
commaTextIo = new CommaTextIo(fileitemdata,#io_write, 65001);
where 65001 is UTF-8
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,188 Super User 2024 Season 2
Martin Dráb 230,030 Most Valuable Professional
nmaenpaa 101,156