开发者

strip carriage returns from xml serialization in F#

开发者 https://www.devze.com 2023-02-10 07:16 出处:网络
update: some background - i use the xml file to generate a set of pdfs (through a java application that drives JasperReports). all the reports are coming out blank when I use this new xml file. I\'ve

update: some background - i use the xml file to generate a set of pdfs (through a java application that drives JasperReports). all the reports are coming out blank when I use this new xml file. I've ruled out network problems because I use an old xml file from the same server that I run the java application with the new xml file. I've compared the two files (old-good one and new-bad one) using a hex-editor and my first clue is that there are carriage returns in the new file and none in the old one. this may not fix the issue, but I'd like to eliminate it from the equation.

I think I need to re开发者_如何学运维move all the carriage returns from my xml file in order for it to work as I need it to. In my travels, the closest I found is this:

.Replace("\r","")

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

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()


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

You should be able to something like this (don't have FSI at hand right now, 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)


It's probably not the best solution, but you could try

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

精彩评论

暂无评论...
验证码 换一张
取 消