简体   繁体   中英

Failed to convert xml to csv using perl script through c#

I have the following code for converting xml to csv by using perl script. When I run the perl script through c# but there is no files are created and string output become empty. What is the problem? I have the perl script with .txt extention, Is this ok or not?

string filePath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + "/Files/SAVVIS_CDR_1806012231.XML";

if (Path.GetExtension(filePath) != "csv")
            {
                ProcessStartInfo perlStartInfo = new ProcessStartInfo(@"C:\Strawberry\perl\bin\perl.exe");
                string perlScriptFilePath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + "/PerlScript/formatter_savvis.pl.txt";
                string csvFilePath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + "/PerlScript/";
                perlStartInfo.Arguments = perlScriptFilePath + " " + filePath + " " + csvFilePath;
                perlStartInfo.UseShellExecute = false;
                perlStartInfo.RedirectStandardOutput = true;
                perlStartInfo.RedirectStandardError = true;
                perlStartInfo.CreateNoWindow = false;

                Process perl = new Process();
                perl.StartInfo = perlStartInfo;
                perl.Start();
                perl.WaitForExit();
                string output = perl.StandardOutput.ReadToEnd();
            }

Could you please anyone help me to solve this problem? Thanks in advance.

First, to find out what went wrong:

string error = perl.StandardError.ReadToEnd();

Also, make sure you have necessary permissions to create files in the output directory. You may try to run your process with Admin privileges to find out if it's a permission issue:

perlStartInfo.Verb = "runas";

You may want to run your entire host process with elevated permissions for this.

(This is only to figure out if it's a permission issue! If it's the case, grant the output directory the necessary permissions, and if possible don't automatically run scripts with admin privileges in production environment)

There also may be errors in the perl script itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM