C# 中 XMLwriter 的缩进和换行命令

发布于 2024-09-30 13:21:26 字数 470 浏览 7 评论 0原文

我正在将一些数据写入 XML 文件...但是当我打开它时,所有值都在一行中...如何以可读格式写入它?即每个节点都在新行和缩进中?

FileStream fs = new FileStream("myfile.xml", FileMode.Create);

XmlWriter w = XmlWriter.Create(fs);

w.WriteStartDocument();
w.WriteStartElement("myfile");                 

w.WriteElementString("id", id.Text);
w.WriteElementString("date", dateTimePicker1.Text);
w.WriteElementString("version", ver.Text);
w.WriteEndElement();
w.WriteEndDocument();
w.Flush();
fs.Close();

I am writing some data to XML file...but when I open it all the values are in a single line...how can write it in readable format?ie each node in new line and indentation?

FileStream fs = new FileStream("myfile.xml", FileMode.Create);

XmlWriter w = XmlWriter.Create(fs);

w.WriteStartDocument();
w.WriteStartElement("myfile");                 

w.WriteElementString("id", id.Text);
w.WriteElementString("date", dateTimePicker1.Text);
w.WriteElementString("version", ver.Text);
w.WriteEndElement();
w.WriteEndDocument();
w.Flush();
fs.Close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情绪 2024-10-07 13:21:26

使用 XmlTextWriter 而不是 XmlWriter,然后设置 Indentation 属性。

示例

string filename = "MyFile.xml";

using (FileStream fileStream = new FileStream(filename, FileMode.Create))
using (StreamWriter sw = new StreamWriter(fileStream))
using (XmlTextWriter xmlWriter = new XmlTextWriter(sw))
{
  xmlWriter.Formatting = Formatting.Indented;
  xmlWriter.Indentation = 4;

  // ... Write elements
}

在 @jumbo 注释之后,这也可以像在 .NET 2 中一样实现。

var filename = "MyFile.xml";
var settings = new XmlWriterSettings() 
{
    Indent = true,
    IndentChars = "    "
}

using (var w = XmlWriter.Create(filename, settings))
{
    // ... Write elements
}

Use a XmlTextWriter instead of XmlWriter and then set the Indentation properties.

Example

string filename = "MyFile.xml";

using (FileStream fileStream = new FileStream(filename, FileMode.Create))
using (StreamWriter sw = new StreamWriter(fileStream))
using (XmlTextWriter xmlWriter = new XmlTextWriter(sw))
{
  xmlWriter.Formatting = Formatting.Indented;
  xmlWriter.Indentation = 4;

  // ... Write elements
}

Following @jumbo comment, this could also be implemented like in .NET 2.

var filename = "MyFile.xml";
var settings = new XmlWriterSettings() 
{
    Indent = true,
    IndentChars = "    "
}

using (var w = XmlWriter.Create(filename, settings))
{
    // ... Write elements
}
拒绝两难 2024-10-07 13:21:26

您需要首先创建一个指定缩进的 XmlWriterSettings 对象,然后在创建 XmlWriter 时,在路径后面传入 XmlWriterSettings。

此外,我使用 using 块让 C# 处理资源的处置,这样我就不必担心在异常时丢失任何资源。

{
  XmlWriterSettings xmlWriterSettings = new XmlWriterSettings()
  {
    Indent = true,
    IndentChars = "\t",
    NewLineOnAttributes = true
  };

  using (XmlWriter w= XmlWriter.Create("myfile.xml", xmlWriterSettings))
  {
    w.WriteStartDocument();
    w.WriteStartElement("myfile");

    w.WriteElementString("id", id.Text);
    w.WriteElementString("date", dateTimePicker1.Text);
    w.WriteElementString("version", ver.Text);
    w.WriteEndElement();
    w.WriteEndDocument();
  }
}

You need to first create an XmlWriterSettings object that specifies your indentation, then when creating your XmlWriter, pass in the XmlWriterSettings after your path.

Additionally, I use the using block to let C# handle the disposing of my resources so that I don't need to worry about losing any resources on an exception.

{
  XmlWriterSettings xmlWriterSettings = new XmlWriterSettings()
  {
    Indent = true,
    IndentChars = "\t",
    NewLineOnAttributes = true
  };

  using (XmlWriter w= XmlWriter.Create("myfile.xml", xmlWriterSettings))
  {
    w.WriteStartDocument();
    w.WriteStartElement("myfile");

    w.WriteElementString("id", id.Text);
    w.WriteElementString("date", dateTimePicker1.Text);
    w.WriteElementString("version", ver.Text);
    w.WriteEndElement();
    w.WriteEndDocument();
  }
}
水溶 2024-10-07 13:21:26

检查设置属性:

w.Settings.Indent = true;

编辑:您不能直接设置它:

System.Xml.XmlWriter.Create("path", new System.Xml.XmlWriterSettings())

Check the Settings property:

w.Settings.Indent = true;

Edit: You can't set it directly:

System.Xml.XmlWriter.Create("path", new System.Xml.XmlWriterSettings())
四叶草在未来唯美盛开 2024-10-07 13:21:26

设置 XmlTextWriter 的格式属性:

TextWriter textWriter;
var xmlTextWriter = new XmlTextWriter(textWriter);
xmlTextWriter.Formatting = Formatting.Indented;

Set the Formatting Property of the XmlTextWriter:

TextWriter textWriter;
var xmlTextWriter = new XmlTextWriter(textWriter);
xmlTextWriter.Formatting = Formatting.Indented;
我为君王 2024-10-07 13:21:26

使用设置如下:

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Encoding = Encoding.UTF8;
        settings.Indent = true;

        using (MemoryStream memoryStream = new MemoryStream())
        using (XmlWriter xmlDoc = XmlWriter.Create(memoryStream, settings)){
         // logic here..
        }

这将让您到达您想要的位置,但是,您不必使用MemoryStream,重要的部分是设置。

Use Settings As follow:

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Encoding = Encoding.UTF8;
        settings.Indent = true;

        using (MemoryStream memoryStream = new MemoryStream())
        using (XmlWriter xmlDoc = XmlWriter.Create(memoryStream, settings)){
         // logic here..
        }

This will get you where you want, though, you don't have to use MemoryStream, the importent part is the settings.

明媚殇 2024-10-07 13:21:26

使用 XmlWriter 和 XmlWriterSettings 直接写入文件的小型 VB.NET 版本:

Dim oSerializer As New XmlSerializer(GetType(MyType), New XmlRootAttribute("MyRootAttributeName"))
Using oXmlWriter As XmlWriter = XmlWriter.Create("MyFilePath", New XmlWriterSettings With {.Indent = True}) //Default encoding is utf-8
    oSerializer.Serialize(oXmlWriter, oInstanceOfMyType)
    oXmlWriter.Flush()
End Using

检查 doco 中的其他设置和文件覆盖选项,但这很干净,并且在很多情况下都可以按预期工作。

A little VB.NET version using XmlWriter plus XmlWriterSettings direct to file:

Dim oSerializer As New XmlSerializer(GetType(MyType), New XmlRootAttribute("MyRootAttributeName"))
Using oXmlWriter As XmlWriter = XmlWriter.Create("MyFilePath", New XmlWriterSettings With {.Indent = True}) //Default encoding is utf-8
    oSerializer.Serialize(oXmlWriter, oInstanceOfMyType)
    oXmlWriter.Flush()
End Using

Check the doco for other settings and file overwrite options but this is clean and works as expected in lots of situations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文