为什么WriteElementString()没有在xml文件中添加新行?

时间:2013-05-27 19:17:38

标签: c# xml

        xmlWriter.WriteStartElement("Video");
        xmlWriter.WriteElementString("FileName", temp.nameFile);
        xmlWriter.WriteElementString("VideoName", temp.nameVideo);
        xmlWriter.WriteElementString("Path", temp.path);
        xmlWriter.WriteEndElement();

我用c#编写了这部分代码,但是当我要检查结果时,我只看到一行没有任何新行!

怎么可能?

非常感谢!

2 个答案:

答案 0 :(得分:1)

如果你想在你的xml编写器上添加一个新行,那么你应该像这样使用xml设置:

XmlWriterSettings settings = new XmlWriterSettings();
//this will auto indent as well as put everything on a new line
settings.Indent = true; 

XmlWriter writer = XmlWriter.Create("file_Name.xml", settings);
writer.WriteStartDocument();

xmlWriter.WriteStartElement("Video");
xmlWriter.WriteElementString("FileName", temp.nameFile);
xmlWriter.WriteElementString("VideoName", temp.nameVideo);
xmlWriter.WriteElementString("Path", temp.path);
xmlWriter.WriteEndElement();

writer.WriteEndDocument();
writer.Flush();
writer.Close();

希望这有帮助

答案 1 :(得分:0)

您可能希望在构建xml编写器时传入设置,使其行为符合您的要求。有关详情,请查看 this