The caret string returns from xml serialization in F #

update: some background - I am using an XML file to create a set of PDF files (via a java application that runs JasperReports). all reports are blank when I use this new XML file. I ruled out problems with the network because I am using the old xml file from the same server as I am running the java application with the new XML file. I matched two files (old-good and new-bad) using a hex editor, and my first key is that there are carriage returns in the new file, and not one in the old one. this may not solve the problem, but I would like to exclude it from the equation.

It seems to me that I need to remove all carriages from my xml file so that it works the way I need it. In my travels, the closest I found is the following:

.Replace("\r","")

but where to use it in the following code? I create my data model, create a root directory and pass it to the serializer. At what point can I say "delete carriage return?"

let def = new reportDefinition("decileRank", "jasper", new template("\\\\server\\location\\filename.jrxml", "jrxml"))
let header = new reportDefinitions([| def |])
let root = reportGenerator(header, new dbConnection(), new reports(reportsArray))

let path = sprintf "C:\\JasperRpt\\parameter_files\\%s\\%d\\%s\\%s\\" report year pmFirm pmName //(System.DateTime.Now.ToString("ddMMyyyy")) 
Directory.CreateDirectory(path) |> ignore
let filename = sprintf "%s%s" path month
printfn "%s" filename     
use fs = new FileStream(filename, FileMode.Create) 
let xmlSerializer = XmlSerializer(typeof<reportGenerator>)    
xmlSerializer.Serialize(fs,root)
fs.Close()
+3
source share
2 answers

XmlWriterSettings has some options for formatting the output, so pass the result through XmlWriter.

You should be able to something like this (don't have FSI on hand right now, I don't know if it compiles. :)

 //use fs = new FileStream(filename, FileMode.Create) 
 let settings = new XmlWriterSettings();
 settings.Indent <- true;
 settings.NewLineChars <- "\n";
 use w = XmlWriter.Create(filename, settings);
 let xmlSerializer = XmlSerializer(typeof<reportGenerator>)    
 xmlSerializer.Serialize(w,root)
+2
source

This is probably not the best solution, but you can try

// after your current code
let xmlString = File.ReadAllText filename
ignore( File.WriteAllText( filename , xmlString.Replace("\r","")))
+2
source

Source: https://habr.com/ru/post/1791328/


All Articles