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 XmlWriter
is 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