在 F# 中从 xml 序列化中去除回车符

发布于 2024-10-17 09:17:28 字数 1110 浏览 2 评论 0原文

更新:一些背景 - 我使用 xml 文件生成一组 pdf(通过驱动 JasperReports 的 java 应用程序)。当我使用这个新的 xml 文件时,所有报告都显示为空白。我已经排除了网络问题,因为我使用来自同一服务器的旧 xml 文件,该服务器与新 xml 文件运行 java 应用程序。我使用十六进制编辑器比较了两个文件(旧的好文件和新的坏文件),我的第一个线索是新文件中有回车符,旧文件中没有回车符。这可能无法解决问题,但我想将其从等式中消除。

认为我需要从 xml 文件中删除所有回车符,以便它能够按我需要的方式工作。在我的旅行中,我发现的最接近的是:

.Replace("\r","")

但是我在下面的代码中在哪里使用它?我创建数据模型,创建根,并将其传递给序列化器。什么时候我可以说“删除回车符”?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月流歌 2024-10-24 09:17:28

XmlWriterSettings 有一些用于格式化输出的选项,因此通过 XmlWriter 传递输出。

你应该能够做到这样的事情(现在手边没有 FSI,不知道它是否可以编译。:)

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

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)
倒数 2024-10-24 09:17:28

这可能不是最好的解决方案,但你可以尝试

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

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","")))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文