How to turn ON/OFF an XmlWriter Indent property?

吃可爱长大的小学妹 提交于 2019-12-25 05:15:47

问题


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>

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.

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.

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.

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.

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

回答1:


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:

  • 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



回答2:


You can not set XmlWriter's indent setting. but you can pass the xmlSetting object to Write_SongInfo_Node procedure and set xmlSetting .Indent = True or False hope it works.

Regards,



来源:https://stackoverflow.com/questions/19540246/how-to-turn-on-off-an-xmlwriter-indent-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!