将字符串写入XML文件而不进行格式化(C#) [英] Writing string to XML file without formatting (C#)

查看:53
本文介绍了将字符串写入XML文件而不进行格式化(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个函数,该函数根据保存在应用程序中的数据构建XML字符串.接下来,应将此字符串写入实际的XML文件,以便可以使用它.然后,此文件将在HTML Web应用程序中用于提供数据.我正在使用以下代码片段来做到这一点:

I wrote a function that builds an XML string based on the data saved in my application. Next up, this string should be written to an actual XML file, so it can be used. This file is then to be used in an HTML web application to feed data. I'm using the following snippet of code to do just that:

xmlDoc.Save("exercise.xml");

很容易,但是有一个小问题.我的XML文件在Firefox中无法正常工作,因为它将空格视为childNode.重写我的整个Web应用程序几乎是不行的,因为这会花费很多工作.我宁愿能够以无格式的方式将XML字符串保存到XML文件中,因为我已经测试并确认该方法几乎可以在所有可能的浏览器中使用.字符串本身不包含任何回车符或制表符,因此Save()方法可能会自动添加它.有什么方法可以防止它发生这种情况,或者有另一种简单的方法可以避免这种情况发生?

Easy enough, but there's a small catch. My XML file won't work properly in Firefox, because it considers white space as a childNode. Rewriting my entire web application is pretty much a no go, as it'd be too much work. I'd rather just be able to save my XML string to an XML file in a non-formatted way, since I've tested and confirmed that this works in just about every conceivable browser. The string itself doesn't contain any carriage returns or tabs, so the Save()-method probably adds it automatically. Any way to prevent it from doing that, or another easy way to circumvent this?

推荐答案

尝试

xDoc.PreserveWhitespace = true;
xDoc.Save(...);

这篇关于将字符串写入XML文件而不进行格式化(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆