הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Wednesday, May 11, 2011 1:28 PM
How do I remove new line and whitespace when using xmlWriter to create an XML file? I would like the file to be as small as possible.
All replies (8)
Thursday, May 12, 2011 7:11 AM ✅Answered
Ok you win.
:)
Using the XmlWriter class with default settings will help you achieve what you want.
Dim doc As New XmlDocument
Dim filename As String
Using writer As XmlWriter = XmlWriter.Create(filename)
doc.Save(writer)
End Using
/Calle
- Still confused, but on a higher level -
Wednesday, May 11, 2011 3:31 PM
Use for instance the System.IO.Compression.GZipStream to compress the stream before writing to file. That would make the file really small. Besides, if you would remove all formatting such as whitespace and line breaks from the xml it wouldn't be human readable anyway.
/Calle
- Still confused, but on a higher level -
Wednesday, May 11, 2011 5:06 PM
Hi, the file will only be used as input for another system. Would I have to uncompress the file if I use GZipStream? The file does not have to be human readable. Removing all whitespace and newline characters would be suitable for this situation.
Wednesday, May 11, 2011 7:54 PM
I believe there is a flag or property that you can set to remove the unnecessary whitespace; it's been a while since I've used XML writers and readers (I'm using JSON for serialized data transfer). If it doesn't have to be humanly readable, you may want to consider binary serialization, or 'roll your own'. Note that they all have shortcomings in their own way.Stephen J Whiteley
Wednesday, May 11, 2011 8:21 PM
Yes, you would need to uncompress as well. Here's a sample:
Sub SaveToFile(ByRef doc As XmlDocument, ByVal filename As String)
Using s As Stream = File.Create(filename)
Using zipStream As New GZipStream(s, CompressionMode.Compress)
doc.Save(zipStream)
End Using
End Using
End Sub
Function LoadFromFile(ByVal filename As String) As XmlDocument
Dim doc As New XmlDocument
Dim zipStream As GZipStream
Using s As Stream = File.OpenRead(filename)
zipStream = New GZipStream(s, CompressionMode.Decompress)
doc.Load(zipStream)
End Using
Return doc
End Function
/Calle
- Still confused, but on a higher level -
Wednesday, May 11, 2011 9:16 PM
Is there another way to remove the newline characters and spaces? I can remove spaces from strings but I'm not sure how to save the XML document to a string and write the XML file after the characters are replaced. I tried setting PreserveWhitespace = False but I still see the newline characters.
Friday, May 20, 2011 1:56 AM
Thanks guys, your posts have helped a bunch.
Friday, May 20, 2011 4:53 AM
Brian,
Be aware that there are endless ways to write a XML file in Net and it is completely depended from the method you use if a linefeed is used.
To remove a whitespace you should use the Trim when you are putting it in the xml.
However, I'm curious how big your XML file is, as a current medium device can hold text data as big as in past a building that contains the complete archive from most big companies since 100 years in text.
And as soon as it becomes images in XML that linefeed becomes completely irrelevant.
So what is the reason you spent time to do this.
Success
Cor