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>
I tried to save some elements from my application in xml file but when I started to develop it using this code :
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();
}
I found that the output in notepad are in one line like this :
<?xml version="1.0" encoding="utf-8"?><cmap
xmlns:dcterms="http://purl.org/dc/terms/"><xmlns>http://cmap.ihmc.us/xml/cmap/</xmlns></cmap>
I want it appear as multiline like this:
<?xml version="1.0" encoding="utf-8"?> <cmap
xmlns:dcterms="http://purl.org/dc/terms/"><xmlns>http://cmap.ihmc.us/xml/cmap/</xmlns>
</cmap>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已创建 XmlWriterSettings 的实例。
You have create an instance of XmlWriterSettings.
您应该使用 XmlWriterSettings - 设置适当的格式选项并在创建 XmlWriter 时传递它。
在这里阅读更多相关信息:http://msdn.microsoft.com/en-us /library/kbef2xz3.aspx
You should use an XmlWriterSettings - set your appropriate formatting options and pass it when creating the XmlWriter.
Read more about it here: http://msdn.microsoft.com/en-us/library/kbef2xz3.aspx