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