C# xmlwriter在一行中写入元素

C# xmlwriter在一行中写入元素,c#,xml,xmlwriter,C#,Xml,Xmlwriter,我试图将应用程序中的一些元素保存在xml文件中,但当我开始使用以下代码开发它时: public static void WriteInFile(string savefilepath) { XmlWriter writer = XmlWriter.Create(savefilepath); WriteXMLFile(writer); } private static void WriteXMLFile(XmlWri

我试图将应用程序中的一些元素保存在xml文件中,但当我开始使用以下代码开发它时:

public static void WriteInFile(string savefilepath)
        {
            XmlWriter writer = XmlWriter.Create(savefilepath);
            WriteXMLFile(writer);

        }
private static void WriteXMLFile(XmlWriter writer) //Write and Create XML profile for specific type 
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("cmap");
            writer.WriteAttributeString("xmlns", "dcterms",null, "http://purl.org/dc/terms/");
            writer.WriteElementString("xmlns", "http://cmap.ihmc.us/xml/cmap/");
           // writer.WriteAttributeString("xmlns","dc",null, "http://purl.org/dc/elements/1.1/");
            //writer.WriteAttributeString("xmlns", "vcard", null, "http://www.w3.org/2001/vcard-rdf/3.0#");
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
        }
我发现记事本中的输出在一行中,如下所示:

<?xml version="1.0" encoding="utf-8"?><cmap
xmlns:dcterms="http://purl.org/dc/terms/"><xmlns>http://cmap.ihmc.us/xml/cmap/</xmlns></cmap>
http://cmap.ihmc.us/xml/cmap/
我希望它显示为多行,如下所示:

<?xml version="1.0" encoding="utf-8"?> <cmap
xmlns:dcterms="http://purl.org/dc/terms/"><xmlns>http://cmap.ihmc.us/xml/cmap/</xmlns>
</cmap>
http://cmap.ihmc.us/xml/cmap/

您已经创建了


您应该使用XmlWriterSettings-设置适当的格式选项,并在创建XmlWriter时传递它


请在此处阅读更多信息:

您想要的输出完全相同。尝试在XMLEditor和/或Visual Studio中加载它。记事本的格式选项并不为人所知。值得一提的是,对于任何有此问题的未来读者来说,书写空白将阻止新行/缩进的工作。MSDN for—“只要元素不包含混合内容,元素就会缩进。一旦调用WriteString或WriteWhitespace方法写出混合元素内容,XmlWriter就会停止缩进。一旦混合内容元素关闭,缩进就会恢复。”
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
XmlWriter writer = XmlWriter.Create(savefilepath, settings);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (var writer = XmlWriter.Create(savefilepath, settings))
{
     WriteXMLFile(writer);
}