将字符串格式化为 XML 以包含制表符、换行符等
我正在将字符串从多行文本框序列化为 XML。是否发送换行符和制表符以保存在 XML 中,以便当我反序列化它时,我得到的结果看起来是一样的?
示例 - 文本框将如下所示:
Hello JohnDoe,
This is a message
...
Somelines....
Thank you,
...
End
当我序列化它然后反序列化它时,文本框的填充方式如下:
Hello JohnDoe,This is a message..Some lines....Thank you,...End
它不必非常漂亮,我只是想想出一个好方法将其重新输入更好的格式。谢谢。
好的,我的代码如下:
public void SerializeToXML(List<Report> newReport)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Report>));
//XmlWriterSettings serializer_settings = new XmlWriterSettings();
//serializer_settings.Indent = true;
//serializer_settings.NewLineOnAttributes = true;
TextWriter textWriter = new StreamWriter(xmlPath);
serializer.Serialize(textWriter, newReport);
textWriter.Close();
}
其中 Report
只是一个带有一些字符串属性的类。注释行是我试图实现我的目标的东西。
报告类代码:
public class Report
{
public string name
{ get; set; }
public string email
{ get; set; }
public string defectID
{ get; set; }
public string fixedBuild
{ get; set; }
public string description
{ get; set; }
[XmlIgnore]
public string messageBody
{ get; set; }
private static readonly XmlDocument _xmlDoc = new XmlDocument();
[XmlElement("messageBody")]
public XmlCDataSection TextCData
{
get
{
return _xmlDoc.CreateCDataSection(messageBody);
}
set
{
messageBody = value.Data;
}
}
}
输出保持不变(无空格格式)。
I am serializing a string from a multiline text box to an XML. Is send the line breaks and tabs to save in the XML so when I deserialize it, I get it back looking the same?
Example - The textbox will look like:
Hello JohnDoe,
This is a message
...
Somelines....
Thank you,
...
End
When I serialize this and then deserialize it, the textbox gets populated like:
Hello JohnDoe,This is a message..Some lines....Thank you,...End
It doesn't have to be really pretty, I'm just trying to come up with a good way to get it back in a better format. Thanks.
Okay so the code i have is as follows:
public void SerializeToXML(List<Report> newReport)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Report>));
//XmlWriterSettings serializer_settings = new XmlWriterSettings();
//serializer_settings.Indent = true;
//serializer_settings.NewLineOnAttributes = true;
TextWriter textWriter = new StreamWriter(xmlPath);
serializer.Serialize(textWriter, newReport);
textWriter.Close();
}
Where Report
is just a class with some string attributes. The commented lines are somethings I was trying to accomplish my goal.
Report class code:
public class Report
{
public string name
{ get; set; }
public string email
{ get; set; }
public string defectID
{ get; set; }
public string fixedBuild
{ get; set; }
public string description
{ get; set; }
[XmlIgnore]
public string messageBody
{ get; set; }
private static readonly XmlDocument _xmlDoc = new XmlDocument();
[XmlElement("messageBody")]
public XmlCDataSection TextCData
{
get
{
return _xmlDoc.CreateCDataSection(messageBody);
}
set
{
messageBody = value.Data;
}
}
}
the output remains the same (no whitespace formatting).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保留 XML 中所有空白的一种方法是使用
CDATA
部分。详细信息取决于您进行序列化的方式。有关解释,请参阅此问题 Web 服务上下文。如果手动序列化,请使用XmlWriter.WriteCData
方法。One way to preserve all whitespace in your XML is to use a
CDATA
section. The details depend on the way you do your serialization. See this question for an explanation in a web service context. If you serialize manually, useXmlWriter.WriteCData
method.这篇文章似乎提供了一个有效的解决方案。另请查看此处的答案。
在没有看到你的代码的情况下很难给出好的建议。
编辑
您可以添加它
或者您也许可以使用类似的东西(未经测试,从此修改 问题):
这样它应该在序列化过程中自动添加。但是,如果您能找出一种不基于字段或属性来设置 XmlAttribute 的方法,那就更好了。
This article seems to offer a valid solution. Also check out this answer here.
Hard to give good advice without seeing your code.
EDIT
You could add it manually.
Or you might be able to get by with something like this (untested, modified from this question):
this way it should get added automatically during serialization. However, if you can figure out a way to set an XmlAttribute that's not based off a field or property, that would be even better.