Adding carriage returns to XML file

fatron

Member
Joined
May 13, 2010
Messages
18
Programming Experience
1-3
I'm working on a program that stores some of its configuration information in an XML file. I'm able to read and write the xml files ok and everything works, but I would like to be able to open the xml files in other programs such as programmer's notepad. When I open the xml file in programmer's notepad, all the text is on a single line.

Is there a way to embed carriage returns into the xml file?

Here's an example of how I'm writing the xml file:
VB.NET:
DataManifest = New XmlDocument
Dim myxmldeclaration As XmlDeclaration
myxmldeclaration = DataManifest.CreateXmlDeclaration("1.0", "UTF-8", "yes")
DataManifest.AppendChild(myxmldeclaration)
root = DataManifest.CreateElement("FILE_INFORMATION")
subchild = DataManifest.CreateElement("VERSION")
subchild.InnerText = "2.0"
root.AppendChild(subchild)
subchild = DataManifest.CreateElement("RECORD_DATE")
subchild.InnerText = Now.ToString("yyyyMMdd")
root.AppendChild(subchild)
subchild = DataManifest.CreateElement("RECORD_TIME")
subchild.InnerText = Now.ToString("HH:mm:ss")
root.AppendChild(subchild)
childevents = DataManifest.CreateElement("EVENTS")
root.AppendChild(childevents)
DataManifest.AppendChild(root)
Dim output As New XmlTextWriter(mediapath + "EVENT_MANIFEST.xml", System.Text.Encoding.UTF8)
DataManifest.WriteTo(output)
output.Close()

Thanks
 
set the formatting of that XmlTextWriter you have... like so:

VB.NET:
Dim writer As New XmlTextWriter(mediapath + "EVENT_MANIFEST.xml", System.Text.Encoding.UTF8)
writer.Formatting = Formatting.Indented
DataManifest.Save(writer)
 
The default writer also indents:
VB.NET:
DataManifest.Save(filepath)
 
While that gets the job done I think using XML literals makes for a much cleaner approach. You can tell at a glance exactly what the resultant file will look like.

VB.NET:
        Dim DataManifest = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                           <FILE_INFORMATION>
                               <VERSION>2.0</VERSION>
                               <RECORD_DATE><%= Now.ToString("yyyyMMdd") %></RECORD_DATE>
                               <RECORD_TIME><%= Now.ToString("HH:mm:ss") %></RECORD_TIME>
                               <EVENTS/>
                           </FILE_INFORMATION>
        DataManifest.Save(mediapath & "EVENT_MANIFEST.xml")
 
Back
Top