gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 01:48:52 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com