如何向 SyndicateFeed 对象的序列化输出添加换行符?
我正在使用 SynminationFeed 类来生成 Atom feed 和 Atom10FeedFormatter 以对其进行序列化。 我希望能够在文件写入磁盘时在元素之间添加换行符。 我意识到提要读者并不关心,但是当我通过 http://feedvalidator.org/ 运行我的文档时它将整个文档视为一行,这使得它成为一个 PITA,可以查看我的错误在哪里,因为每个错误都在“第 1 行”上。
例如,而不是这样的输出:
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">Title For My Feed</title><subtitle type="text">Subtitle for my feed.</subtitle><id>uuid:d2ad3f53-6f1a-4495-ba92-ab3231413f97;id=1</id><updated>2009-05-12T19:42:56Z</updated><author><name>Matt</name>...
我想得到这样的输出:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">Title For My Feed</title>
<subtitle type="text">Subtitle for my feed.</subtitle>
<id>uuid:d2ad3f53-6f1a-4495-ba92-ab3231413f97;id=1</id>
<updated>2009-05-12T19:42:56Z</updated>
<author>
<name>Matt</name>
...
这是我用来序列化的代码,以防万一它很重要:
XmlWriter atomWriter = XmlWriter.Create(@"atom.xml");
Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(TheFeed);
atomFormatter.WriteTo(atomWriter);
atomWriter.Close();
I'm using the SyndicationFeed class to generate an Atom feed and an Atom10FeedFormatter to serialize it. I'd like to be able to add line breaks between the elements when the file gets written to disk. I realize the feed readers don't care, but when I run my docs through http://feedvalidator.org/ it treats the whole doc as a single line, which makes it a PITA to see where my mistakes are since every error is on "line 1".
For example, instead of output like this:
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">Title For My Feed</title><subtitle type="text">Subtitle for my feed.</subtitle><id>uuid:d2ad3f53-6f1a-4495-ba92-ab3231413f97;id=1</id><updated>2009-05-12T19:42:56Z</updated><author><name>Matt</name>...
I'd like to get output something like this:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">Title For My Feed</title>
<subtitle type="text">Subtitle for my feed.</subtitle>
<id>uuid:d2ad3f53-6f1a-4495-ba92-ab3231413f97;id=1</id>
<updated>2009-05-12T19:42:56Z</updated>
<author>
<name>Matt</name>
...
Here's the code I'm using to serialize, just in case it matters:
XmlWriter atomWriter = XmlWriter.Create(@"atom.xml");
Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(TheFeed);
atomFormatter.WriteTo(atomWriter);
atomWriter.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XmlWriter 类有一个 Settings 属性,可让您以多种方式设置 xml 格式,包括行格式和缩进。
以下是 MSDN 参考。
The XmlWriter class has a Settings property that lets you format your xml in a number of ways, including line formatting and also indentation.
Here's the MSDN reference.
约瑟夫的回答因向我指出了正确的班级而受到赞誉。 以下是其他寻找该代码的人的具体信息。
我所要做的就是告诉 XmlWriter 对象缩进输出。 通过将设置对象的 Indent 属性更改为 true 可以轻松实现这一点。 因此,我用于序列化提要(上面)的原始代码是这样编辑的:
Joseph's answer gets the credit for pointing me at the correct class. What follows are the specifics for anyone else looking for teh codez.
All I had to do was tell the XmlWriter object to indent the output. That was easily accomplished by changing the Indent property of the settings object to true. So my original code to serialize the feed (above) was edited like so: