如何打开/关闭 XmlWriter Indent 属性? [英] How to turn ON/OFF an XmlWriter Indent property?

查看:29
本文介绍了如何打开/关闭 XmlWriter Indent 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在需要时打开/关闭 XmlWriterIndent 属性,以便能够使用这种格式进行编写:

I would like to turn ON/OFF the Indent property of a XmlWriter when I need it, to be able to write with this formatting:

<?xml version="1.0" encoding="Windows-1252"?>
<Songs>
  <Song><Name>My Song 1.mp3</Name><Year>2007</Year></Song>
  <Song><Name>My Song 2.mp3</Name><Year>2009</Year></Song>
  <Song><Name>My Song 3.mp3</Name><Year>2008</Year></Song>
</Songs>

问题是我不知道如何修改属性,我在其他 SO 问题中读到了不可能多次设置此属性的问题...但是...如果使用 XmlTextWriter 而不是 XmlWriter 我可以随时打开/关闭缩进,并注意到 XmlTextWriter 来自 Framework 2.0 所以我不敢相信有无法修改 Indent 属性,因为 XmlWriter 类比 XmlTextWriter 更新得多.

The problem is I don't know how to modify the property, I've read in other SO question that is not possibly to set this property more than once... but... if using a XmlTextWriter instead an XmlWriter I can turn on/off the Indentation whenever i want, and notice that a XmlTextWriter is from Framework 2.0 so I can not believe that there is no way to modify the Indent property since the Class XmlWriteris much more current than the XmlTextWriter.

我不会使用 XmlTextWriter 而不是 XmlWriter 仅仅因为 XmlWriter 在未来几天可能会完全过时,我更喜欢使用框架当前类的逻辑,而不是将我的代码基于旧类.

I would not use the XmlTextWriter instead of the XmlWriter just because the XmlWriter could become totally obsolete on future days, I preffer to use the logic of Framework current classes and to not base my code on old Classes.

此外,我不会做一些棘手的事情,例如使用 WriteString 方法禁用缩进和换行",因为这会在某些行中引发异常.

Also I would not to do tricky things like "disable Indent and wrtie newlines with the WriteString method", because that will throws me exceptions at certains lines.

澄清一下,我只是想打开/关闭 XmlWriter 的缩进属性或一种可以随时打开/关闭缩进的虚拟和工作方式,但不是我想要的任何其他东西不会.

To clarify, I just would to turn on/off the indent property of an XmlWriter or a devired and working way to turn on/off indent whenever I want, but not any other thing that I won't do.

这是代码,当我尝试修改 Indent 属性时它抛出异常,说它只是读取属性,但就像我说的那样,我可以在 XmlTextWriter 中做到这一点...这似乎太奇怪了.

Here is the Code, it throws an exception when I attempt to modify the Indent property saying that is only read property, but like I've said, I can do it in a XmlTextWriter so... this seems too strange.

Dim xmlsettings As XmlWriterSettings = _
    New XmlWriterSettings() With { _
        .Indent = True, _
        .Encoding = System.Text.Encoding.Default}

Dim Xml As XmlWriter = XmlTextWriter.Create(xmlfile, xmlsettings)

Private Sub Write_SongInfo_Node(ByVal xml As XmlWriter,
                  ByVal Name As String, _
                  ByVal Year As String)

    xml.WriteStartElement("Song")

    xml.Settings.Indent = False

    xml.WriteStartElement("Name")
    xml.WriteString(Name)
    xml.WriteEndElement()

    xml.WriteStartElement("Year")
    xml.WriteString(Year)
    xml.WriteEndElement()

    xml.Settings.Indent = True

    xml.WriteEndElement()

    ' xml.WriteString(Environment.NewLine)

End Sub

推荐答案

XmlWriterSettings 在您创建编写器实例时立即应用于整个编写器,然后切换到只读模式.只有一种可能的方法可以关闭此模式 - 使用 XmlWriterSettings.Reset() 方法,但我不确定它是否可以帮助您.XmlTextWriter 有自己的缩进格式选项,它会在每个元素写入步骤中检查它们.换句话说,XmlTextWriter 使用自己的函数编写缩进而不使用 xmlwriter 缩进属性.因此,您只有一个可行的决定 - 为元素编写实现自定义逻辑,您可以:

XmlWriterSettings is applied for whole writer at once when you create your writer instance and than it switches to read-only mode. There is only one possible way to switch this mode off - use XmlWriterSettings.Reset() method but I'm not sure that it can help you. XmlTextWriter have got own formatting options for indentation and it checks them on each element writing step. In other words XmlTextWriter writes indents using own functional and doesn't use xmlwriter indentation properties. So there is only one working decision for you - implement custom logic for elements writing in which you can:

  • 覆盖默认行为,例如检查元素名称(如Song")并省略无用的缩进符号
  • 关闭 XmlWriter 缩进 (XmlWriterSettings.Indent = false ) 并添加您自己的方法,该方法将在您需要时手动编写缩进
  • override default behavior, e.g. check elements name (like "Song") and omit useless indent symbols
  • switch off XmlWriter indents (XmlWriterSettings.Indent = false ) and add your own method which will write indents manually when you need it

这篇关于如何打开/关闭 XmlWriter Indent 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆