c# - xmlwriter 在一行中写入元素

标签 c# xml xmlwriter

我试图将我的应用程序中的一些元素保存在 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>

我希望它像这样显示为多行:

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

最佳答案

您已经创建了 XmlWriterSettings. 的实例

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
XmlWriter writer = XmlWriter.Create(savefilepath, settings);

关于c# - xmlwriter 在一行中写入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8521268/

相关文章:

c# - 如何结束进程?

python - 提取文本 w.r.t 属性值,lxml findall

java - DocumentBuilder.parse 引发错误 "The prefix "c“元素 "c:de"未绑定(bind)。”

php - 使用 XMLWriter 生成 XML 文件

iphone - objective-c XMLWriter

c# - 没有 UI 线程的任务同步

c# - 编译器只会编译可以执行的代码吗?

c# - 方法未显示在 Swagger UI(带 Swashbuckle)中,但没有错误消息

c# - 属性的 XML 反序列化

c# - 编写 XSLT 样式表 header